legup-4.0
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
llvm::InstCombiner Class Reference

InstCombiner - The -instcombine pass. More...

#include <InstCombine.h>

+ Inheritance diagram for llvm::InstCombiner:

Public Types

typedef IRBuilder< true,
TargetFolder,
InstCombineIRInserter
BuilderTy
 

Public Member Functions

 InstCombiner ()
 
bool runOnFunction (Function &F) override
 
bool DoOneIteration (Function &F, unsigned ItNum)
 
void getAnalysisUsage (AnalysisUsage &AU) const override
 
const DataLayoutgetDataLayout () const
 
TargetLibraryInfogetTargetLibraryInfo () const
 
InstructionvisitAdd (BinaryOperator &I)
 
InstructionvisitFAdd (BinaryOperator &I)
 
ValueOptimizePointerDifference (Value *LHS, Value *RHS, Type *Ty)
 
InstructionvisitSub (BinaryOperator &I)
 
InstructionvisitFSub (BinaryOperator &I)
 
InstructionvisitMul (BinaryOperator &I)
 
ValuefoldFMulConst (Instruction *FMulOrDiv, Constant *C, Instruction *InsertBefore)
 
InstructionvisitFMul (BinaryOperator &I)
 
InstructionvisitURem (BinaryOperator &I)
 
InstructionvisitSRem (BinaryOperator &I)
 
InstructionvisitFRem (BinaryOperator &I)
 
bool SimplifyDivRemOfSelect (BinaryOperator &I)
 
InstructioncommonRemTransforms (BinaryOperator &I)
 
InstructioncommonIRemTransforms (BinaryOperator &I)
 Common integer remainder transforms. More...
 
InstructioncommonDivTransforms (BinaryOperator &I)
 
InstructioncommonIDivTransforms (BinaryOperator &I)
 Common integer divide transforms. More...
 
InstructionvisitUDiv (BinaryOperator &I)
 
InstructionvisitSDiv (BinaryOperator &I)
 
InstructionvisitFDiv (BinaryOperator &I)
 
ValueFoldAndOfICmps (ICmpInst *LHS, ICmpInst *RHS)
 FoldAndOfICmps - Fold (icmp)&(icmp) if possible. More...
 
ValueFoldAndOfFCmps (FCmpInst *LHS, FCmpInst *RHS)
 
InstructionvisitAnd (BinaryOperator &I)
 
ValueFoldOrOfICmps (ICmpInst *LHS, ICmpInst *RHS)
 FoldOrOfICmps - Fold (icmp)|(icmp) if possible. More...
 
ValueFoldOrOfFCmps (FCmpInst *LHS, FCmpInst *RHS)
 
InstructionFoldOrWithConstants (BinaryOperator &I, Value *Op, Value *A, Value *B, Value *C)
 
InstructionvisitOr (BinaryOperator &I)
 
InstructionvisitXor (BinaryOperator &I)
 
InstructionvisitShl (BinaryOperator &I)
 
InstructionvisitAShr (BinaryOperator &I)
 
InstructionvisitLShr (BinaryOperator &I)
 
InstructioncommonShiftTransforms (BinaryOperator &I)
 
InstructionFoldFCmp_IntToFP_Cst (FCmpInst &I, Instruction *LHSI, Constant *RHSC)
 
InstructionFoldCmpLoadFromIndexedGlobal (GetElementPtrInst *GEP, GlobalVariable *GV, CmpInst &ICI, ConstantInt *AndCst=nullptr)
 
InstructionvisitFCmpInst (FCmpInst &I)
 
InstructionvisitICmpInst (ICmpInst &I)
 
InstructionvisitICmpInstWithCastAndCast (ICmpInst &ICI)
 
InstructionvisitICmpInstWithInstAndIntCst (ICmpInst &ICI, Instruction *LHS, ConstantInt *RHS)
 
InstructionFoldICmpDivCst (ICmpInst &ICI, BinaryOperator *DivI, ConstantInt *DivRHS)
 
InstructionFoldICmpShrCst (ICmpInst &ICI, BinaryOperator *DivI, ConstantInt *DivRHS)
 FoldICmpShrCst - Handle "icmp(([al]shr X, cst1), cst2)". More...
 
InstructionFoldICmpAddOpCst (Instruction &ICI, Value *X, ConstantInt *CI, ICmpInst::Predicate Pred)
 FoldICmpAddOpCst - Fold "icmp pred (X+CI), X". More...
 
InstructionFoldGEPICmp (GEPOperator *GEPLHS, Value *RHS, ICmpInst::Predicate Cond, Instruction &I)
 
InstructionFoldShiftByConstant (Value *Op0, Constant *Op1, BinaryOperator &I)
 
InstructioncommonCastTransforms (CastInst &CI)
 Implement the transforms common to all CastInst visitors. More...
 
InstructioncommonPointerCastTransforms (CastInst &CI)
 Implement the transforms for cast of pointer (bitcast/ptrtoint) More...
 
InstructionvisitTrunc (TruncInst &CI)
 
InstructionvisitZExt (ZExtInst &CI)
 
InstructionvisitSExt (SExtInst &CI)
 
InstructionvisitFPTrunc (FPTruncInst &CI)
 
InstructionvisitFPExt (CastInst &CI)
 
InstructionvisitFPToUI (FPToUIInst &FI)
 
InstructionvisitFPToSI (FPToSIInst &FI)
 
InstructionvisitUIToFP (CastInst &CI)
 
InstructionvisitSIToFP (CastInst &CI)
 
InstructionvisitPtrToInt (PtrToIntInst &CI)
 
InstructionvisitIntToPtr (IntToPtrInst &CI)
 
InstructionvisitBitCast (BitCastInst &CI)
 
InstructionvisitAddrSpaceCast (AddrSpaceCastInst &CI)
 
InstructionFoldSelectOpOp (SelectInst &SI, Instruction *TI, Instruction *FI)
 
InstructionFoldSelectIntoOp (SelectInst &SI, Value *, Value *)
 
InstructionFoldSPFofSPF (Instruction *Inner, SelectPatternFlavor SPF1, Value *A, Value *B, Instruction &Outer, SelectPatternFlavor SPF2, Value *C)
 
InstructionvisitSelectInst (SelectInst &SI)
 
InstructionvisitSelectInstWithICmp (SelectInst &SI, ICmpInst *ICI)
 
InstructionvisitCallInst (CallInst &CI)
 
InstructionvisitInvokeInst (InvokeInst &II)
 
InstructionSliceUpIllegalIntegerPHI (PHINode &PN)
 
InstructionvisitPHINode (PHINode &PN)
 
InstructionvisitGetElementPtrInst (GetElementPtrInst &GEP)
 
InstructionvisitAllocaInst (AllocaInst &AI)
 
InstructionvisitAllocSite (Instruction &FI)
 
InstructionvisitFree (CallInst &FI)
 
InstructionvisitLoadInst (LoadInst &LI)
 
InstructionvisitStoreInst (StoreInst &SI)
 
InstructionvisitBranchInst (BranchInst &BI)
 
InstructionvisitSwitchInst (SwitchInst &SI)
 
InstructionvisitInsertValueInst (InsertValueInst &IV)
 
InstructionvisitInsertElementInst (InsertElementInst &IE)
 
InstructionvisitExtractElementInst (ExtractElementInst &EI)
 
InstructionvisitShuffleVectorInst (ShuffleVectorInst &SVI)
 
InstructionvisitExtractValueInst (ExtractValueInst &EV)
 
InstructionvisitLandingPadInst (LandingPadInst &LI)
 
InstructionvisitInstruction (Instruction &I)
 
InstructionInsertNewInstBefore (Instruction *New, Instruction &Old)
 
InstructionInsertNewInstWith (Instruction *New, Instruction &Old)
 
InstructionReplaceInstUsesWith (Instruction &I, Value *V)
 
InstructionEraseInstFromFunction (Instruction &I)
 
void computeKnownBits (Value *V, APInt &KnownZero, APInt &KnownOne, unsigned Depth=0) const
 
bool MaskedValueIsZero (Value *V, const APInt &Mask, unsigned Depth=0) const
 
unsigned ComputeNumSignBits (Value *Op, unsigned Depth=0) const
 
PasscreatePrinterPass (raw_ostream &O, const std::string &Banner) const override
 createPrinterPass - Get a function printer pass. More...
 
void assignPassManager (PMStack &PMS, PassManagerType T) override
 
PassManagerType getPotentialPassManagerType () const override
 Return what kind of Pass Manager can manage this pass. More...
 
PassKind getPassKind () const
 
virtual const char * getPassName () const
 
AnalysisID getPassID () const
 getPassID - Return the PassID number that corresponds to this pass. More...
 
virtual bool doInitialization (Module &)
 
virtual bool doFinalization (Module &)
 
virtual void print (raw_ostream &O, const Module *M) const
 
void dump () const
 
virtual void preparePassManager (PMStack &)
 Check if available pass managers are suitable for this pass or not. More...
 
void setResolver (AnalysisResolver *AR)
 
AnalysisResolvergetResolver () const
 
virtual void releaseMemory ()
 
virtual void * getAdjustedAnalysisPointer (AnalysisID ID)
 
virtual ImmutablePassgetAsImmutablePass ()
 
virtual PMDataManagergetAsPMDataManager ()
 
virtual void verifyAnalysis () const
 
virtual void dumpPassStructure (unsigned Offset=0)
 
template<typename AnalysisType >
AnalysisType * getAnalysisIfAvailable () const
 
bool mustPreserveAnalysisID (char &AID) const
 
template<typename AnalysisType >
AnalysisType & getAnalysis () const
 
template<typename AnalysisType >
AnalysisType & getAnalysis (Function &F)
 
template<typename AnalysisType >
AnalysisType & getAnalysisID (AnalysisID PI) const
 
template<typename AnalysisType >
AnalysisType & getAnalysisID (AnalysisID PI, Function &F)
 
void visit (Iterator Start, Iterator End)
 
void visit (Module &M)
 
void visit (Function &F)
 
void visit (BasicBlock &BB)
 
void visit (Module *M)
 
void visit (Function *F)
 
void visit (BasicBlock *BB)
 
Instructionvisit (Instruction *I)
 
Instructionvisit (Instruction &I)
 
void visitModule (Module &M)
 
void visitFunction (Function &F)
 
void visitBasicBlock (BasicBlock &BB)
 
InstructionvisitReturnInst (ReturnInst &I)
 
InstructionvisitIndirectBrInst (IndirectBrInst &I)
 
InstructionvisitResumeInst (ResumeInst &I)
 
InstructionvisitUnreachableInst (UnreachableInst &I)
 
InstructionvisitAtomicCmpXchgInst (AtomicCmpXchgInst &I)
 
InstructionvisitAtomicRMWInst (AtomicRMWInst &I)
 
InstructionvisitFenceInst (FenceInst &I)
 
InstructionvisitTruncInst (TruncInst &I)
 
InstructionvisitZExtInst (ZExtInst &I)
 
InstructionvisitSExtInst (SExtInst &I)
 
InstructionvisitFPTruncInst (FPTruncInst &I)
 
InstructionvisitFPExtInst (FPExtInst &I)
 
InstructionvisitFPToUIInst (FPToUIInst &I)
 
InstructionvisitFPToSIInst (FPToSIInst &I)
 
InstructionvisitUIToFPInst (UIToFPInst &I)
 
InstructionvisitSIToFPInst (SIToFPInst &I)
 
InstructionvisitPtrToIntInst (PtrToIntInst &I)
 
InstructionvisitIntToPtrInst (IntToPtrInst &I)
 
InstructionvisitBitCastInst (BitCastInst &I)
 
InstructionvisitAddrSpaceCastInst (AddrSpaceCastInst &I)
 
InstructionvisitVAArgInst (VAArgInst &I)
 
InstructionvisitDbgDeclareInst (DbgDeclareInst &I)
 
InstructionvisitDbgValueInst (DbgValueInst &I)
 
InstructionvisitDbgInfoIntrinsic (DbgInfoIntrinsic &I)
 
InstructionvisitMemSetInst (MemSetInst &I)
 
InstructionvisitMemCpyInst (MemCpyInst &I)
 
InstructionvisitMemMoveInst (MemMoveInst &I)
 
InstructionvisitMemTransferInst (MemTransferInst &I)
 
InstructionvisitMemIntrinsic (MemIntrinsic &I)
 
InstructionvisitVAStartInst (VAStartInst &I)
 
InstructionvisitVAEndInst (VAEndInst &I)
 
InstructionvisitVACopyInst (VACopyInst &I)
 
InstructionvisitIntrinsicInst (IntrinsicInst &I)
 
InstructionvisitCastInst (CastInst &I)
 
InstructionvisitBinaryOperator (BinaryOperator &I)
 
InstructionvisitCmpInst (CmpInst &I)
 
InstructionvisitTerminatorInst (TerminatorInst &I)
 
InstructionvisitUnaryInstruction (UnaryInstruction &I)
 

Static Public Member Functions

static const PassInfolookupPassInfo (const void *TI)
 
static const PassInfolookupPassInfo (StringRef Arg)
 
static PasscreatePass (AnalysisID ID)
 

Data Fields

InstCombineWorklist Worklist
 Worklist - All of the instructions that need to be simplified. More...
 
BuilderTyBuilder
 

Static Public Attributes

static char ID = 0
 

Protected Member Functions

bool skipOptnoneFunction (const Function &F) const
 

Private Member Functions

bool ShouldChangeType (Type *From, Type *To) const
 
Valuedyn_castNegVal (Value *V) const
 
Valuedyn_castFNegVal (Value *V, bool NoSignedZero=false) const
 
TypeFindElementAtOffset (Type *PtrTy, int64_t Offset, SmallVectorImpl< Value * > &NewIndices)
 
InstructionFoldOpIntoSelect (Instruction &Op, SelectInst *SI)
 
bool ShouldOptimizeCast (Instruction::CastOps opcode, const Value *V, Type *Ty)
 
InstructionvisitCallSite (CallSite CS)
 
InstructiontryOptimizeCall (CallInst *CI, const DataLayout *DL)
 
bool transformConstExprCastCall (CallSite CS)
 
InstructiontransformCallThroughTrampoline (CallSite CS, IntrinsicInst *Tramp)
 
InstructiontransformZExtICmp (ICmpInst *ICI, Instruction &CI, bool DoXform=true)
 
InstructiontransformSExtICmp (ICmpInst *ICI, Instruction &CI)
 
bool WillNotOverflowSignedAdd (Value *LHS, Value *RHS)
 
bool WillNotOverflowUnsignedAdd (Value *LHS, Value *RHS)
 
ValueEmitGEPOffset (User *GEP)
 
InstructionscalarizePHI (ExtractElementInst &EI, PHINode *PN)
 
ValueEvaluateInDifferentElementOrder (Value *V, ArrayRef< int > Mask)
 
bool SimplifyAssociativeOrCommutative (BinaryOperator &I)
 
ValueSimplifyUsingDistributiveLaws (BinaryOperator &I)
 
ValueSimplifyDemandedUseBits (Value *V, APInt DemandedMask, APInt &KnownZero, APInt &KnownOne, unsigned Depth)
 
bool SimplifyDemandedBits (Use &U, APInt DemandedMask, APInt &KnownZero, APInt &KnownOne, unsigned Depth=0)
 
ValueSimplifyShrShlDemandedBits (Instruction *Lsr, Instruction *Sftl, APInt DemandedMask, APInt &KnownZero, APInt &KnownOne)
 
bool SimplifyDemandedInstructionBits (Instruction &Inst)
 
ValueSimplifyDemandedVectorElts (Value *V, APInt DemandedElts, APInt &UndefElts, unsigned Depth=0)
 
ValueSimplifyVectorOp (BinaryOperator &Inst)
 Makes transformation of binary operation specific for vector types. More...
 
InstructionFoldOpIntoPhi (Instruction &I)
 
InstructionFoldPHIArgOpIntoPHI (PHINode &PN)
 
InstructionFoldPHIArgBinOpIntoPHI (PHINode &PN)
 
InstructionFoldPHIArgGEPIntoPHI (PHINode &PN)
 
InstructionFoldPHIArgLoadIntoPHI (PHINode &PN)
 
InstructionOptAndOp (Instruction *Op, ConstantInt *OpRHS, ConstantInt *AndRHS, BinaryOperator &TheAnd)
 
ValueFoldLogicalPlusAnd (Value *LHS, Value *RHS, ConstantInt *Mask, bool isSub, Instruction &I)
 
ValueInsertRangeTest (Value *V, Constant *Lo, Constant *Hi, bool isSigned, bool Inside)
 
InstructionPromoteCastOfAllocation (BitCastInst &CI, AllocaInst &AI)
 
InstructionMatchBSwap (BinaryOperator &I)
 
bool SimplifyStoreAtEndOfBlock (StoreInst &SI)
 
InstructionSimplifyMemTransfer (MemIntrinsic *MI)
 
InstructionSimplifyMemSet (MemSetInst *MI)
 
ValueEvaluateInDifferentType (Value *V, Type *Ty, bool isSigned)
 
ValueDescale (Value *Val, APInt Scale, bool &NoSignedWrap)
 

Private Attributes

const DataLayoutDL
 
TargetLibraryInfoTLI
 
bool MadeIRChange
 
LibCallSimplifierSimplifier
 
bool MinimizeSize
 

Detailed Description

InstCombiner - The -instcombine pass.

Definition at line 86 of file InstCombine.h.

Member Typedef Documentation

Builder - This is an IRBuilder that automatically inserts new instructions into the worklist when they are created.

Definition at line 101 of file InstCombine.h.

Constructor & Destructor Documentation

llvm::InstCombiner::InstCombiner ( )
inline

Definition at line 105 of file InstCombine.h.

105  : FunctionPass(ID), DL(nullptr), Builder(nullptr) {
106  MinimizeSize = false;
108  }
void initializeInstCombinerPass(PassRegistry &)
static PassRegistry * getPassRegistry()
BuilderTy * Builder
Definition: InstCombine.h:102
const DataLayout * DL
Definition: InstCombine.h:89
FunctionPass(char &pid)
Definition: Pass.h:296

Member Function Documentation

void FunctionPass::assignPassManager ( PMStack PMS,
PassManagerType  PreferredType 
)
overridevirtualinherited

Find appropriate Function Pass Manager or Call Graph Pass Manager in the PM Stack and add self into that manager.

Reimplemented from llvm::Pass.

Definition at line 1865 of file LegacyPassManager.cpp.

1866  {
1867 
1868  // Find Function Pass Manager
1869  while (!PMS.empty()) {
1871  PMS.pop();
1872  else
1873  break;
1874  }
1875 
1876  // Create new Function Pass Manager if needed.
1877  FPPassManager *FPP;
1879  FPP = (FPPassManager *)PMS.top();
1880  } else {
1881  assert(!PMS.empty() && "Unable to create Function Pass Manager");
1882  PMDataManager *PMD = PMS.top();
1883 
1884  // [1] Create new Function Pass Manager
1885  FPP = new FPPassManager();
1886  FPP->populateInheritedAnalysis(PMS);
1887 
1888  // [2] Set up new manager's top level manager
1889  PMTopLevelManager *TPM = PMD->getTopLevelManager();
1890  TPM->addIndirectPassManager(FPP);
1891 
1892  // [3] Assign manager to manage this new manager. This may create
1893  // and push new managers into PMS
1894  FPP->assignPassManager(PMS, PMD->getPassManagerType());
1895 
1896  // [4] Push new manager into PMS
1897  PMS.push(FPP);
1898  }
1899 
1900  // Assign FPP as the manager of this pass.
1901  FPP->add(this);
1902 }
virtual PassManagerType getPassManagerType() const
void populateInheritedAnalysis(PMStack &PMS)
void assignPassManager(PMStack &PMS, PassManagerType T) override
void add(Pass *P, bool ProcessAnalysis=true)
assert(Globals.size() > 1)
void addIndirectPassManager(PMDataManager *Manager)
bool empty() const
PMDataManager * top() const
void push(PMDataManager *PM)
FPPassManager.
Definition: Pass.h:59
Instruction * InstCombiner::commonCastTransforms ( CastInst CI)

Implement the transforms common to all CastInst visitors.

Definition at line 294 of file InstCombineCasts.cpp.

294  {
295  Value *Src = CI.getOperand(0);
296 
297  // Many cases of "cast of a cast" are eliminable. If it's eliminable we just
298  // eliminate it now.
299  if (CastInst *CSrc = dyn_cast<CastInst>(Src)) { // A->B->C cast
300  if (Instruction::CastOps opc =
301  isEliminableCastPair(CSrc, CI.getOpcode(), CI.getType(), DL)) {
302  // The first cast (CSrc) is eliminable so we need to fix up or replace
303  // the second cast (CI). CSrc will then have a good chance of being dead.
304  return CastInst::Create(opc, CSrc->getOperand(0), CI.getType());
305  }
306  }
307 
308  // If we are casting a select then fold the cast into the select
309  if (SelectInst *SI = dyn_cast<SelectInst>(Src))
310  if (Instruction *NV = FoldOpIntoSelect(CI, SI))
311  return NV;
312 
313  // If we are casting a PHI then fold the cast into the PHI
314  if (isa<PHINode>(Src)) {
315  // We don't do this if this would create a PHI node with an illegal type if
316  // it is currently legal.
317  if (!Src->getType()->isIntegerTy() ||
318  !CI.getType()->isIntegerTy() ||
319  ShouldChangeType(CI.getType(), Src->getType()))
320  if (Instruction *NV = FoldOpIntoPhi(CI))
321  return NV;
322  }
323 
324  return nullptr;
325 }
Instruction * FoldOpIntoPhi(Instruction &I)
Instruction::CastOps getOpcode() const
Return the opcode of this CastInst.
Definition: InstrTypes.h:624
Instruction * FoldOpIntoSelect(Instruction &Op, SelectInst *SI)
Base class of casting instructions.
Definition: InstrTypes.h:387
Value * getOperand(unsigned i) const
Definition: User.h:90
Type * getType() const
Definition: Value.h:215
bool isIntegerTy() const
Definition: Type.h:193
static CastInst * Create(Instruction::CastOps, Value *S, Type *Ty, const Twine &Name="", Instruction *InsertBefore=nullptr)
Construct any of the CastInst subclasses.
bool ShouldChangeType(Type *From, Type *To) const
const DataLayout * DL
Definition: InstCombine.h:89
LLVM Value Representation.
Definition: Value.h:69
static Instruction::CastOps isEliminableCastPair(const CastInst *CI, unsigned opcode, Type *DstTy, const DataLayout *DL)
Instruction* llvm::InstCombiner::commonDivTransforms ( BinaryOperator I)
Instruction * InstCombiner::commonIDivTransforms ( BinaryOperator I)

Common integer divide transforms.

This function implements the transforms common to both integer division instructions (udiv and sdiv). It is called by the visitors to those integer division instructions.

Definition at line 713 of file InstCombineMulDivRem.cpp.

713  {
714  Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
715 
716  // The RHS is known non-zero.
717  if (Value *V = simplifyValueKnownNonZero(I.getOperand(1), *this)) {
718  I.setOperand(1, V);
719  return &I;
720  }
721 
722  // Handle cases involving: [su]div X, (select Cond, Y, Z)
723  // This does not apply for fdiv.
724  if (isa<SelectInst>(Op1) && SimplifyDivRemOfSelect(I))
725  return &I;
726 
727  if (ConstantInt *RHS = dyn_cast<ConstantInt>(Op1)) {
728  // (X / C1) / C2 -> X / (C1*C2)
729  if (Instruction *LHS = dyn_cast<Instruction>(Op0))
730  if (Instruction::BinaryOps(LHS->getOpcode()) == I.getOpcode())
731  if (ConstantInt *LHSRHS = dyn_cast<ConstantInt>(LHS->getOperand(1))) {
732  if (MultiplyOverflows(RHS, LHSRHS,
733  I.getOpcode() == Instruction::SDiv))
735  return BinaryOperator::Create(I.getOpcode(), LHS->getOperand(0),
736  ConstantExpr::getMul(RHS, LHSRHS));
737  }
738 
739  if (!RHS->isZero()) { // avoid X udiv 0
740  if (SelectInst *SI = dyn_cast<SelectInst>(Op0))
741  if (Instruction *R = FoldOpIntoSelect(I, SI))
742  return R;
743  if (isa<PHINode>(Op0))
744  if (Instruction *NV = FoldOpIntoPhi(I))
745  return NV;
746  }
747  }
748 
749  if (ConstantInt *One = dyn_cast<ConstantInt>(Op0)) {
750  if (One->isOne() && !I.getType()->isIntegerTy(1)) {
751  bool isSigned = I.getOpcode() == Instruction::SDiv;
752  if (isSigned) {
753  // If Op1 is 0 then it's undefined behaviour, if Op1 is 1 then the
754  // result is one, if Op1 is -1 then the result is minus one, otherwise
755  // it's zero.
756  Value *Inc = Builder->CreateAdd(Op1, One);
757  Value *Cmp = Builder->CreateICmpULT(
758  Inc, ConstantInt::get(I.getType(), 3));
759  return SelectInst::Create(Cmp, Op1, ConstantInt::get(I.getType(), 0));
760  } else {
761  // If Op1 is 0 then it's undefined behaviour. If Op1 is 1 then the
762  // result is one, otherwise it's zero.
763  return new ZExtInst(Builder->CreateICmpEQ(Op1, One), I.getType());
764  }
765  }
766  }
767 
768  // See if we can fold away this div instruction.
770  return &I;
771 
772  // (X - (X rem Y)) / Y -> X / Y; usually originates as ((X / Y) * Y) / Y
773  Value *X = nullptr, *Z = nullptr;
774  if (match(Op0, m_Sub(m_Value(X), m_Value(Z)))) { // (X - Z) / Y; Y = Op1
775  bool isSigned = I.getOpcode() == Instruction::SDiv;
776  if ((isSigned && match(Z, m_SRem(m_Specific(X), m_Specific(Op1)))) ||
777  (!isSigned && match(Z, m_URem(m_Specific(X), m_Specific(Op1)))))
778  return BinaryOperator::Create(I.getOpcode(), X, Op1);
779  }
780 
781  return nullptr;
782 }
Instruction * FoldOpIntoPhi(Instruction &I)
class_match< Value > m_Value()
m_Value() - Match an arbitrary value and ignore it.
Definition: PatternMatch.h:70
BinaryOp_match< LHS, RHS, Instruction::Sub > m_Sub(const LHS &L, const RHS &R)
Definition: PatternMatch.h:407
bool SimplifyDivRemOfSelect(BinaryOperator &I)
BinaryOp_match< LHS, RHS, Instruction::SRem > m_SRem(const LHS &L, const RHS &R)
Definition: PatternMatch.h:455
This class represents zero extension of integer types.
static RegisterPass< NVPTXAllocaHoisting > X("alloca-hoisting","Hoisting alloca instructions in non-entry ""blocks to the entry block")
Value * CreateICmpULT(Value *LHS, Value *RHS, const Twine &Name="")
Definition: IRBuilder.h:1259
Instruction * FoldOpIntoSelect(Instruction &Op, SelectInst *SI)
Value * CreateICmpEQ(Value *LHS, Value *RHS, const Twine &Name="")
Definition: IRBuilder.h:1247
static Value * simplifyValueKnownNonZero(Value *V, InstCombiner &IC)
static Constant * getNullValue(Type *Ty)
Definition: Constants.cpp:133
bool match(Val *V, const Pattern &P)
Definition: PatternMatch.h:42
static SelectInst * Create(Value *C, Value *S1, Value *S2, const Twine &NameStr="", Instruction *InsertBefore=nullptr)
Value * CreateAdd(Value *LHS, Value *RHS, const Twine &Name="", bool HasNUW=false, bool HasNSW=false)
Definition: IRBuilder.h:632
Instruction * ReplaceInstUsesWith(Instruction &I, Value *V)
Definition: InstCombine.h:282
specificval_ty m_Specific(const Value *V)
m_Specific - Match if we have a specific specified value.
Definition: PatternMatch.h:323
Value * getOperand(unsigned i) const
Definition: User.h:90
BuilderTy * Builder
Definition: InstCombine.h:102
BinaryOps getOpcode() const
Definition: InstrTypes.h:326
Class for constant integers.
Definition: Constants.h:51
Type * getType() const
Definition: Value.h:215
BinaryOp_match< LHS, RHS, Instruction::URem > m_URem(const LHS &L, const RHS &R)
Definition: PatternMatch.h:449
static Constant * get(Type *Ty, uint64_t V, bool isSigned=false)
Definition: Constants.cpp:528
void setOperand(unsigned i, Value *Val)
Definition: User.h:94
static RegisterPass< SwOnly > Z("legup-sw-only","Replace accelerated functions with wrappers and produce legup_wrappers")
bool isIntegerTy() const
Definition: Type.h:193
static BinaryOperator * Create(BinaryOps Op, Value *S1, Value *S2, const Twine &Name=Twine(), Instruction *InsertBefore=nullptr)
#define I(x, y, z)
Definition: MD5.cpp:54
LLVM Value Representation.
Definition: Value.h:69
bool SimplifyDemandedInstructionBits(Instruction &Inst)
static bool MultiplyOverflows(ConstantInt *C1, ConstantInt *C2, bool sign)
static Constant * getMul(Constant *C1, Constant *C2, bool HasNUW=false, bool HasNSW=false)
Definition: Constants.cpp:2124
Instruction * InstCombiner::commonIRemTransforms ( BinaryOperator I)

Common integer remainder transforms.

This function implements the transforms common to both integer remainder instructions (urem and srem). It is called by the visitors to those integer remainder instructions.

Definition at line 1194 of file InstCombineMulDivRem.cpp.

1194  {
1195  Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
1196 
1197  // The RHS is known non-zero.
1198  if (Value *V = simplifyValueKnownNonZero(I.getOperand(1), *this)) {
1199  I.setOperand(1, V);
1200  return &I;
1201  }
1202 
1203  // Handle cases involving: rem X, (select Cond, Y, Z)
1204  if (isa<SelectInst>(Op1) && SimplifyDivRemOfSelect(I))
1205  return &I;
1206 
1207  if (isa<Constant>(Op1)) {
1208  if (Instruction *Op0I = dyn_cast<Instruction>(Op0)) {
1209  if (SelectInst *SI = dyn_cast<SelectInst>(Op0I)) {
1210  if (Instruction *R = FoldOpIntoSelect(I, SI))
1211  return R;
1212  } else if (isa<PHINode>(Op0I)) {
1213  if (Instruction *NV = FoldOpIntoPhi(I))
1214  return NV;
1215  }
1216 
1217  // See if we can fold away this rem instruction.
1219  return &I;
1220  }
1221  }
1222 
1223  return nullptr;
1224 }
Instruction * FoldOpIntoPhi(Instruction &I)
bool SimplifyDivRemOfSelect(BinaryOperator &I)
Instruction * FoldOpIntoSelect(Instruction &Op, SelectInst *SI)
static Value * simplifyValueKnownNonZero(Value *V, InstCombiner &IC)
Value * getOperand(unsigned i) const
Definition: User.h:90
void setOperand(unsigned i, Value *Val)
Definition: User.h:94
#define I(x, y, z)
Definition: MD5.cpp:54
LLVM Value Representation.
Definition: Value.h:69
bool SimplifyDemandedInstructionBits(Instruction &Inst)
Instruction * InstCombiner::commonPointerCastTransforms ( CastInst CI)

Implement the transforms for cast of pointer (bitcast/ptrtoint)

Definition at line 1431 of file InstCombineCasts.cpp.

1431  {
1432  Value *Src = CI.getOperand(0);
1433 
1434  if (GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(Src)) {
1435  // If casting the result of a getelementptr instruction with no offset, turn
1436  // this into a cast of the original pointer!
1437  if (GEP->hasAllZeroIndices() &&
1438  // If CI is an addrspacecast and GEP changes the poiner type, merging
1439  // GEP into CI would undo canonicalizing addrspacecast with different
1440  // pointer types, causing infinite loops.
1441  (!isa<AddrSpaceCastInst>(CI) ||
1442  GEP->getType() == GEP->getPointerOperand()->getType())) {
1443  // Changing the cast operand is usually not a good idea but it is safe
1444  // here because the pointer operand is being replaced with another
1445  // pointer operand so the opcode doesn't need to change.
1446  Worklist.Add(GEP);
1447  CI.setOperand(0, GEP->getOperand(0));
1448  return &CI;
1449  }
1450 
1451  if (!DL)
1452  return commonCastTransforms(CI);
1453 
1454  // If the GEP has a single use, and the base pointer is a bitcast, and the
1455  // GEP computes a constant offset, see if we can convert these three
1456  // instructions into fewer. This typically happens with unions and other
1457  // non-type-safe code.
1458  unsigned AS = GEP->getPointerAddressSpace();
1459  unsigned OffsetBits = DL->getPointerSizeInBits(AS);
1460  APInt Offset(OffsetBits, 0);
1461  BitCastInst *BCI = dyn_cast<BitCastInst>(GEP->getOperand(0));
1462  if (GEP->hasOneUse() &&
1463  BCI &&
1464  GEP->accumulateConstantOffset(*DL, Offset)) {
1465  // Get the base pointer input of the bitcast, and the type it points to.
1466  Value *OrigBase = BCI->getOperand(0);
1467  SmallVector<Value*, 8> NewIndices;
1468  if (FindElementAtOffset(OrigBase->getType(),
1469  Offset.getSExtValue(),
1470  NewIndices)) {
1471  // If we were able to index down into an element, create the GEP
1472  // and bitcast the result. This eliminates one bitcast, potentially
1473  // two.
1474  Value *NGEP = cast<GEPOperator>(GEP)->isInBounds() ?
1475  Builder->CreateInBoundsGEP(OrigBase, NewIndices) :
1476  Builder->CreateGEP(OrigBase, NewIndices);
1477  NGEP->takeName(GEP);
1478 
1479  if (isa<BitCastInst>(CI))
1480  return new BitCastInst(NGEP, CI.getType());
1481  assert(isa<PtrToIntInst>(CI));
1482  return new PtrToIntInst(NGEP, CI.getType());
1483  }
1484  }
1485  }
1486 
1487  return commonCastTransforms(CI);
1488 }
Value * CreateGEP(Value *Ptr, ArrayRef< Value * > IdxList, const Twine &Name="")
Definition: IRBuilder.h:957
Instruction * commonCastTransforms(CastInst &CI)
Implement the transforms common to all CastInst visitors.
void Add(Instruction *I)
This class represents a cast from a pointer to an integer.
This class represents a no-op cast from one type to another.
void takeName(Value *V)
Definition: Value.cpp:242
assert(Globals.size() > 1)
Value * getOperand(unsigned i) const
Definition: User.h:90
InstCombineWorklist Worklist
Worklist - All of the instructions that need to be simplified.
Definition: InstCombine.h:97
BuilderTy * Builder
Definition: InstCombine.h:102
Value * CreateInBoundsGEP(Value *Ptr, ArrayRef< Value * > IdxList, const Twine &Name="")
Definition: IRBuilder.h:970
Type * getType() const
Definition: Value.h:215
void setOperand(unsigned i, Value *Val)
Definition: User.h:94
Class for arbitrary precision integers.
Definition: APInt.h:75
LLVM_ATTRIBUTE_UNUSED_RESULT std::enable_if< !is_simple_type< Y >::value, typename cast_retty< X, const Y >::ret_type >::type dyn_cast(const Y &Val)
Definition: Casting.h:265
unsigned getPointerSizeInBits(unsigned AS=0) const
Definition: DataLayout.h:313
const DataLayout * DL
Definition: InstCombine.h:89
LLVM Value Representation.
Definition: Value.h:69
Type * FindElementAtOffset(Type *PtrTy, int64_t Offset, SmallVectorImpl< Value * > &NewIndices)
Instruction* llvm::InstCombiner::commonRemTransforms ( BinaryOperator I)
Instruction * InstCombiner::commonShiftTransforms ( BinaryOperator I)

Definition at line 24 of file InstCombineShifts.cpp.

24  {
25  assert(I.getOperand(1)->getType() == I.getOperand(0)->getType());
26  Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
27 
28  // See if we can fold away this shift.
30  return &I;
31 
32  // Try to fold constant and into select arguments.
33  if (isa<Constant>(Op0))
34  if (SelectInst *SI = dyn_cast<SelectInst>(Op1))
35  if (Instruction *R = FoldOpIntoSelect(I, SI))
36  return R;
37 
38  if (Constant *CUI = dyn_cast<Constant>(Op1))
39  if (Instruction *Res = FoldShiftByConstant(Op0, CUI, I))
40  return Res;
41 
42  // X shift (A srem B) -> X shift (A and B-1) iff B is a power of 2.
43  // Because shifts by negative values (which could occur if A were negative)
44  // are undefined.
45  Value *A; const APInt *B;
46  if (Op1->hasOneUse() && match(Op1, m_SRem(m_Value(A), m_Power2(B)))) {
47  // FIXME: Should this get moved into SimplifyDemandedBits by saying we don't
48  // demand the sign bit (and many others) here??
49  Value *Rem = Builder->CreateAnd(A, ConstantInt::get(I.getType(), *B-1),
50  Op1->getName());
51  I.setOperand(1, Rem);
52  return &I;
53  }
54 
55  return nullptr;
56 }
class_match< Value > m_Value()
m_Value() - Match an arbitrary value and ignore it.
Definition: PatternMatch.h:70
BinaryOp_match< LHS, RHS, Instruction::SRem > m_SRem(const LHS &L, const RHS &R)
Definition: PatternMatch.h:455
Instruction * FoldOpIntoSelect(Instruction &Op, SelectInst *SI)
StringRef getName() const
Definition: Value.cpp:168
bool match(Val *V, const Pattern &P)
Definition: PatternMatch.h:42
Value * CreateAnd(Value *LHS, Value *RHS, const Twine &Name="")
Definition: IRBuilder.h:806
cst_pred_ty< is_power2 > m_Power2()
m_Power2() - Match an integer or vector power of 2.
Definition: PatternMatch.h:281
assert(Globals.size() > 1)
LLVM Constant Representation.
Definition: Constant.h:41
Value * getOperand(unsigned i) const
Definition: User.h:90
BuilderTy * Builder
Definition: InstCombine.h:102
Type * getType() const
Definition: Value.h:215
static Constant * get(Type *Ty, uint64_t V, bool isSigned=false)
Definition: Constants.cpp:528
void setOperand(unsigned i, Value *Val)
Definition: User.h:94
Class for arbitrary precision integers.
Definition: APInt.h:75
#define I(x, y, z)
Definition: MD5.cpp:54
LLVM Value Representation.
Definition: Value.h:69
bool SimplifyDemandedInstructionBits(Instruction &Inst)
Instruction * FoldShiftByConstant(Value *Op0, Constant *Op1, BinaryOperator &I)
void llvm::InstCombiner::computeKnownBits ( Value V,
APInt KnownZero,
APInt KnownOne,
unsigned  Depth = 0 
) const
inline

Definition at line 318 of file InstCombine.h.

319  {
320  return llvm::computeKnownBits(V, KnownZero, KnownOne, DL, Depth);
321  }
const DataLayout * DL
Definition: InstCombine.h:89
void computeKnownBits(Value *V, APInt &KnownZero, APInt &KnownOne, const DataLayout *TD=nullptr, unsigned Depth=0)
unsigned llvm::InstCombiner::ComputeNumSignBits ( Value Op,
unsigned  Depth = 0 
) const
inline

Definition at line 327 of file InstCombine.h.

327  {
328  return llvm::ComputeNumSignBits(Op, DL, Depth);
329  }
unsigned ComputeNumSignBits(Value *Op, const DataLayout *TD=nullptr, unsigned Depth=0)
const DataLayout * DL
Definition: InstCombine.h:89
Pass * Pass::createPass ( AnalysisID  ID)
staticinherited

Definition at line 195 of file Pass.cpp.

195  {
197  if (!PI)
198  return nullptr;
199  return PI->createPass();
200 }
static PassRegistry * getPassRegistry()
Pass * createPass() const
createPass() - Use this method to create an instance of this pass.
Definition: PassInfo.h:117
const PassInfo * getPassInfo(const void *TI) const
Pass * FunctionPass::createPrinterPass ( raw_ostream O,
const std::string &  Banner 
) const
overridevirtualinherited

createPrinterPass - Get a function printer pass.

Implements llvm::Pass.

Definition at line 134 of file Pass.cpp.

135  {
136  return createPrintFunctionPass(O, Banner);
137 }
FunctionPass * createPrintFunctionPass(raw_ostream &OS, const std::string &Banner="")
Create and return a pass that prints functions to the specified raw_ostream as they are processed...
Value * InstCombiner::Descale ( Value Val,
APInt  Scale,
bool &  NoSignedWrap 
)
private

Descale - Return a value X such that Val = X * Scale, or null if none. If the multiplication is known not to overflow then NoSignedWrap is set.

Definition at line 940 of file InstructionCombining.cpp.

940  {
941  assert(isa<IntegerType>(Val->getType()) && "Can only descale integers!");
942  assert(cast<IntegerType>(Val->getType())->getBitWidth() ==
943  Scale.getBitWidth() && "Scale not compatible with value!");
944 
945  // If Val is zero or Scale is one then Val = Val * Scale.
946  if (match(Val, m_Zero()) || Scale == 1) {
947  NoSignedWrap = true;
948  return Val;
949  }
950 
951  // If Scale is zero then it does not divide Val.
952  if (Scale.isMinValue())
953  return nullptr;
954 
955  // Look through chains of multiplications, searching for a constant that is
956  // divisible by Scale. For example, descaling X*(Y*(Z*4)) by a factor of 4
957  // will find the constant factor 4 and produce X*(Y*Z). Descaling X*(Y*8) by
958  // a factor of 4 will produce X*(Y*2). The principle of operation is to bore
959  // down from Val:
960  //
961  // Val = M1 * X || Analysis starts here and works down
962  // M1 = M2 * Y || Doesn't descend into terms with more
963  // M2 = Z * 4 \/ than one use
964  //
965  // Then to modify a term at the bottom:
966  //
967  // Val = M1 * X
968  // M1 = Z * Y || Replaced M2 with Z
969  //
970  // Then to work back up correcting nsw flags.
971 
972  // Op - the term we are currently analyzing. Starts at Val then drills down.
973  // Replaced with its descaled value before exiting from the drill down loop.
974  Value *Op = Val;
975 
976  // Parent - initially null, but after drilling down notes where Op came from.
977  // In the example above, Parent is (Val, 0) when Op is M1, because M1 is the
978  // 0'th operand of Val.
979  std::pair<Instruction*, unsigned> Parent;
980 
981  // RequireNoSignedWrap - Set if the transform requires a descaling at deeper
982  // levels that doesn't overflow.
983  bool RequireNoSignedWrap = false;
984 
985  // logScale - log base 2 of the scale. Negative if not a power of 2.
986  int32_t logScale = Scale.exactLogBase2();
987 
988  for (;; Op = Parent.first->getOperand(Parent.second)) { // Drill down
989 
990  if (ConstantInt *CI = dyn_cast<ConstantInt>(Op)) {
991  // If Op is a constant divisible by Scale then descale to the quotient.
992  APInt Quotient(Scale), Remainder(Scale); // Init ensures right bitwidth.
993  APInt::sdivrem(CI->getValue(), Scale, Quotient, Remainder);
994  if (!Remainder.isMinValue())
995  // Not divisible by Scale.
996  return nullptr;
997  // Replace with the quotient in the parent.
998  Op = ConstantInt::get(CI->getType(), Quotient);
999  NoSignedWrap = true;
1000  break;
1001  }
1002 
1003  if (BinaryOperator *BO = dyn_cast<BinaryOperator>(Op)) {
1004 
1005  if (BO->getOpcode() == Instruction::Mul) {
1006  // Multiplication.
1007  NoSignedWrap = BO->hasNoSignedWrap();
1008  if (RequireNoSignedWrap && !NoSignedWrap)
1009  return nullptr;
1010 
1011  // There are three cases for multiplication: multiplication by exactly
1012  // the scale, multiplication by a constant different to the scale, and
1013  // multiplication by something else.
1014  Value *LHS = BO->getOperand(0);
1015  Value *RHS = BO->getOperand(1);
1016 
1017  if (ConstantInt *CI = dyn_cast<ConstantInt>(RHS)) {
1018  // Multiplication by a constant.
1019  if (CI->getValue() == Scale) {
1020  // Multiplication by exactly the scale, replace the multiplication
1021  // by its left-hand side in the parent.
1022  Op = LHS;
1023  break;
1024  }
1025 
1026  // Otherwise drill down into the constant.
1027  if (!Op->hasOneUse())
1028  return nullptr;
1029 
1030  Parent = std::make_pair(BO, 1);
1031  continue;
1032  }
1033 
1034  // Multiplication by something else. Drill down into the left-hand side
1035  // since that's where the reassociate pass puts the good stuff.
1036  if (!Op->hasOneUse())
1037  return nullptr;
1038 
1039  Parent = std::make_pair(BO, 0);
1040  continue;
1041  }
1042 
1043  if (logScale > 0 && BO->getOpcode() == Instruction::Shl &&
1044  isa<ConstantInt>(BO->getOperand(1))) {
1045  // Multiplication by a power of 2.
1046  NoSignedWrap = BO->hasNoSignedWrap();
1047  if (RequireNoSignedWrap && !NoSignedWrap)
1048  return nullptr;
1049 
1050  Value *LHS = BO->getOperand(0);
1051  int32_t Amt = cast<ConstantInt>(BO->getOperand(1))->
1052  getLimitedValue(Scale.getBitWidth());
1053  // Op = LHS << Amt.
1054 
1055  if (Amt == logScale) {
1056  // Multiplication by exactly the scale, replace the multiplication
1057  // by its left-hand side in the parent.
1058  Op = LHS;
1059  break;
1060  }
1061  if (Amt < logScale || !Op->hasOneUse())
1062  return nullptr;
1063 
1064  // Multiplication by more than the scale. Reduce the multiplying amount
1065  // by the scale in the parent.
1066  Parent = std::make_pair(BO, 1);
1067  Op = ConstantInt::get(BO->getType(), Amt - logScale);
1068  break;
1069  }
1070  }
1071 
1072  if (!Op->hasOneUse())
1073  return nullptr;
1074 
1075  if (CastInst *Cast = dyn_cast<CastInst>(Op)) {
1076  if (Cast->getOpcode() == Instruction::SExt) {
1077  // Op is sign-extended from a smaller type, descale in the smaller type.
1078  unsigned SmallSize = Cast->getSrcTy()->getPrimitiveSizeInBits();
1079  APInt SmallScale = Scale.trunc(SmallSize);
1080  // Suppose Op = sext X, and we descale X as Y * SmallScale. We want to
1081  // descale Op as (sext Y) * Scale. In order to have
1082  // sext (Y * SmallScale) = (sext Y) * Scale
1083  // some conditions need to hold however: SmallScale must sign-extend to
1084  // Scale and the multiplication Y * SmallScale should not overflow.
1085  if (SmallScale.sext(Scale.getBitWidth()) != Scale)
1086  // SmallScale does not sign-extend to Scale.
1087  return nullptr;
1088  assert(SmallScale.exactLogBase2() == logScale);
1089  // Require that Y * SmallScale must not overflow.
1090  RequireNoSignedWrap = true;
1091 
1092  // Drill down through the cast.
1093  Parent = std::make_pair(Cast, 0);
1094  Scale = SmallScale;
1095  continue;
1096  }
1097 
1098  if (Cast->getOpcode() == Instruction::Trunc) {
1099  // Op is truncated from a larger type, descale in the larger type.
1100  // Suppose Op = trunc X, and we descale X as Y * sext Scale. Then
1101  // trunc (Y * sext Scale) = (trunc Y) * Scale
1102  // always holds. However (trunc Y) * Scale may overflow even if
1103  // trunc (Y * sext Scale) does not, so nsw flags need to be cleared
1104  // from this point up in the expression (see later).
1105  if (RequireNoSignedWrap)
1106  return nullptr;
1107 
1108  // Drill down through the cast.
1109  unsigned LargeSize = Cast->getSrcTy()->getPrimitiveSizeInBits();
1110  Parent = std::make_pair(Cast, 0);
1111  Scale = Scale.sext(LargeSize);
1112  if (logScale + 1 == (int32_t)Cast->getType()->getPrimitiveSizeInBits())
1113  logScale = -1;
1114  assert(Scale.exactLogBase2() == logScale);
1115  continue;
1116  }
1117  }
1118 
1119  // Unsupported expression, bail out.
1120  return nullptr;
1121  }
1122 
1123  // If Op is zero then Val = Op * Scale.
1124  if (match(Op, m_Zero())) {
1125  NoSignedWrap = true;
1126  return Op;
1127  }
1128 
1129  // We know that we can successfully descale, so from here on we can safely
1130  // modify the IR. Op holds the descaled version of the deepest term in the
1131  // expression. NoSignedWrap is 'true' if multiplying Op by Scale is known
1132  // not to overflow.
1133 
1134  if (!Parent.first)
1135  // The expression only had one term.
1136  return Op;
1137 
1138  // Rewrite the parent using the descaled version of its operand.
1139  assert(Parent.first->hasOneUse() && "Drilled down when more than one use!");
1140  assert(Op != Parent.first->getOperand(Parent.second) &&
1141  "Descaling was a no-op?");
1142  Parent.first->setOperand(Parent.second, Op);
1143  Worklist.Add(Parent.first);
1144 
1145  // Now work back up the expression correcting nsw flags. The logic is based
1146  // on the following observation: if X * Y is known not to overflow as a signed
1147  // multiplication, and Y is replaced by a value Z with smaller absolute value,
1148  // then X * Z will not overflow as a signed multiplication either. As we work
1149  // our way up, having NoSignedWrap 'true' means that the descaled value at the
1150  // current level has strictly smaller absolute value than the original.
1151  Instruction *Ancestor = Parent.first;
1152  do {
1153  if (BinaryOperator *BO = dyn_cast<BinaryOperator>(Ancestor)) {
1154  // If the multiplication wasn't nsw then we can't say anything about the
1155  // value of the descaled multiplication, and we have to clear nsw flags
1156  // from this point on up.
1157  bool OpNoSignedWrap = BO->hasNoSignedWrap();
1158  NoSignedWrap &= OpNoSignedWrap;
1159  if (NoSignedWrap != OpNoSignedWrap) {
1160  BO->setHasNoSignedWrap(NoSignedWrap);
1161  Worklist.Add(Ancestor);
1162  }
1163  } else if (Ancestor->getOpcode() == Instruction::Trunc) {
1164  // The fact that the descaled input to the trunc has smaller absolute
1165  // value than the original input doesn't tell us anything useful about
1166  // the absolute values of the truncations.
1167  NoSignedWrap = false;
1168  }
1169  assert((Ancestor->getOpcode() != Instruction::SExt || NoSignedWrap) &&
1170  "Failed to keep proper track of nsw flags while drilling down?");
1171 
1172  if (Ancestor == Val)
1173  // Got to the top, all done!
1174  return Val;
1175 
1176  // Move up one level in the expression.
1177  assert(Ancestor->hasOneUse() && "Drilled down when more than one use!");
1178  Ancestor = Ancestor->user_back();
1179  } while (1);
1180 }
match_zero m_Zero()
Definition: PatternMatch.h:137
static void sdivrem(const APInt &LHS, const APInt &RHS, APInt &Quotient, APInt &Remainder)
Bitwise AND operator.
Definition: APInt.cpp:1979
void Add(Instruction *I)
bool match(Val *V, const Pattern &P)
Definition: PatternMatch.h:42
static unsigned getBitWidth(Type *Ty, const DataLayout *TD)
Base class of casting instructions.
Definition: InstrTypes.h:387
assert(Globals.size() > 1)
APInt LLVM_ATTRIBUTE_UNUSED_RESULT trunc(unsigned width) const
Truncate to new width.
Definition: APInt.cpp:920
APInt LLVM_ATTRIBUTE_UNUSED_RESULT sext(unsigned width) const
Sign extend to a new width.
Definition: APInt.cpp:943
unsigned getBitWidth() const
Return the number of bits in the APInt.
Definition: APInt.h:1248
InstCombineWorklist Worklist
Worklist - All of the instructions that need to be simplified.
Definition: InstCombine.h:97
Class for constant integers.
Definition: Constants.h:51
Type * getType() const
Definition: Value.h:215
Instruction * user_back()
Definition: Instruction.h:50
static Constant * get(Type *Ty, uint64_t V, bool isSigned=false)
Definition: Constants.cpp:528
int32_t exactLogBase2() const
Definition: APInt.h:1534
Class for arbitrary precision integers.
Definition: APInt.h:75
bool isMinValue() const
Determine if this is the smallest unsigned value.
Definition: APInt.h:363
bool hasOneUse() const
Definition: Value.h:284
LLVM Value Representation.
Definition: Value.h:69
unsigned getOpcode() const
getOpcode() returns a member of one of the enums like Instruction::Add.
Definition: Instruction.h:86
virtual bool llvm::Pass::doFinalization ( Module )
inlinevirtualinherited

doFinalization - Virtual method overriden by subclasses to do any necessary clean up after all passes have run.

Reimplemented in llvm::FPPassManager, llvm::legacy::FunctionPassManagerImpl, llvm::NVPTXAsmPrinter, llvm::MachineModuleInfo, llvm::AsmPrinter, legup::MinimizeBitwidth, legup::SwOnly, legup::LegupPass, and legup::LiveVariableAnalysis.

Definition at line 116 of file Pass.h.

116 { return false; }
virtual bool llvm::Pass::doInitialization ( Module )
inlinevirtualinherited

doInitialization - Virtual method overridden by subclasses to do any necessary initialization before any pass is run.

Reimplemented in llvm::FPPassManager, llvm::legacy::FunctionPassManagerImpl, llvm::NVPTXAsmPrinter, llvm::MachineModuleInfo, llvm::AsmPrinter, legup::MinimizeBitwidth, legup::MemProf, legup::SwOnly, legup::LegupPass, llvm::MachineFunctionAnalysis, legup::LegUp, legup::LegUpTrackBB, legup::LiveVariableAnalysis, legup::LegUpWatch, and legup::PostLTO.

Definition at line 111 of file Pass.h.

111 { return false; }
bool InstCombiner::DoOneIteration ( Function F,
unsigned  ItNum 
)

Definition at line 2638 of file InstructionCombining.cpp.

2638  {
2639  MadeIRChange = false;
2640 
2641  DEBUG(dbgs() << "\n\nINSTCOMBINE ITERATION #" << Iteration << " on "
2642  << F.getName() << "\n");
2643 
2644  {
2645  // Do a depth-first traversal of the function, populate the worklist with
2646  // the reachable instructions. Ignore blocks that are not reachable. Keep
2647  // track of which blocks we visit.
2649  MadeIRChange |= AddReachableCodeToWorklist(F.begin(), Visited, *this, DL,
2650  TLI);
2651 
2652  // Do a quick scan over the function. If we find any blocks that are
2653  // unreachable, remove any instructions inside of them. This prevents
2654  // the instcombine code from having to deal with some bad special cases.
2655  for (Function::iterator BB = F.begin(), E = F.end(); BB != E; ++BB) {
2656  if (Visited.count(BB)) continue;
2657 
2658  // Delete the instructions backwards, as it has a reduced likelihood of
2659  // having to update as many def-use and use-def chains.
2660  Instruction *EndInst = BB->getTerminator(); // Last not to be deleted.
2661  while (EndInst != BB->begin()) {
2662  // Delete the next to last instruction.
2663  BasicBlock::iterator I = EndInst;
2664  Instruction *Inst = --I;
2665  if (!Inst->use_empty())
2666  Inst->replaceAllUsesWith(UndefValue::get(Inst->getType()));
2667  if (isa<LandingPadInst>(Inst)) {
2668  EndInst = Inst;
2669  continue;
2670  }
2671  if (!isa<DbgInfoIntrinsic>(Inst)) {
2672  ++NumDeadInst;
2673  MadeIRChange = true;
2674  }
2675  Inst->eraseFromParent();
2676  }
2677  }
2678  }
2679 
2680  while (!Worklist.isEmpty()) {
2682  if (I == nullptr) continue; // skip null values.
2683 
2684  // Check to see if we can DCE the instruction.
2685  if (isInstructionTriviallyDead(I, TLI)) {
2686  DEBUG(dbgs() << "IC: DCE: " << *I << '\n');
2688  ++NumDeadInst;
2689  MadeIRChange = true;
2690  continue;
2691  }
2692 
2693  // Instruction isn't dead, see if we can constant propagate it.
2694  if (!I->use_empty() && isa<Constant>(I->getOperand(0)))
2695  if (Constant *C = ConstantFoldInstruction(I, DL, TLI)) {
2696  DEBUG(dbgs() << "IC: ConstFold to: " << *C << " from: " << *I << '\n');
2697 
2698  // Add operands to the worklist.
2699  ReplaceInstUsesWith(*I, C);
2700  ++NumConstProp;
2702  MadeIRChange = true;
2703  continue;
2704  }
2705 
2706  // See if we can trivially sink this instruction to a successor basic block.
2707  if (I->hasOneUse()) {
2708  BasicBlock *BB = I->getParent();
2709  Instruction *UserInst = cast<Instruction>(*I->user_begin());
2710  BasicBlock *UserParent;
2711 
2712  // Get the block the use occurs in.
2713  if (PHINode *PN = dyn_cast<PHINode>(UserInst))
2714  UserParent = PN->getIncomingBlock(*I->use_begin());
2715  else
2716  UserParent = UserInst->getParent();
2717 
2718  if (UserParent != BB) {
2719  bool UserIsSuccessor = false;
2720  // See if the user is one of our successors.
2721  for (succ_iterator SI = succ_begin(BB), E = succ_end(BB); SI != E; ++SI)
2722  if (*SI == UserParent) {
2723  UserIsSuccessor = true;
2724  break;
2725  }
2726 
2727  // If the user is one of our immediate successors, and if that successor
2728  // only has us as a predecessors (we'd have to split the critical edge
2729  // otherwise), we can keep going.
2730  if (UserIsSuccessor && UserParent->getSinglePredecessor()) {
2731  // Okay, the CFG is simple enough, try to sink this instruction.
2732  if (TryToSinkInstruction(I, UserParent)) {
2733  MadeIRChange = true;
2734  // We'll add uses of the sunk instruction below, but since sinking
2735  // can expose opportunities for it's *operands* add them to the
2736  // worklist
2737  for (Use &U : I->operands())
2738  if (Instruction *OpI = dyn_cast<Instruction>(U.get()))
2739  Worklist.Add(OpI);
2740  }
2741  }
2742  }
2743  }
2744 
2745  // Now that we have an instruction, try combining it to simplify it.
2748 
2749 #ifndef NDEBUG
2750  std::string OrigI;
2751 #endif
2752  DEBUG(raw_string_ostream SS(OrigI); I->print(SS); OrigI = SS.str(););
2753  DEBUG(dbgs() << "IC: Visiting: " << OrigI << '\n');
2754 
2755  if (Instruction *Result = visit(*I)) {
2756  ++NumCombined;
2757  // Should we replace the old instruction with a new one?
2758  if (Result != I) {
2759  DEBUG(dbgs() << "IC: Old = " << *I << '\n'
2760  << " New = " << *Result << '\n');
2761 
2762  if (!I->getDebugLoc().isUnknown())
2763  Result->setDebugLoc(I->getDebugLoc());
2764  // Everything uses the new instruction now.
2765  I->replaceAllUsesWith(Result);
2766 
2767  // Move the name to the new instruction first.
2768  Result->takeName(I);
2769 
2770  // Push the new instruction and any users onto the worklist.
2771  Worklist.Add(Result);
2772  Worklist.AddUsersToWorkList(*Result);
2773 
2774  // Insert the new instruction into the basic block...
2775  BasicBlock *InstParent = I->getParent();
2776  BasicBlock::iterator InsertPos = I;
2777 
2778  // If we replace a PHI with something that isn't a PHI, fix up the
2779  // insertion point.
2780  if (!isa<PHINode>(Result) && isa<PHINode>(InsertPos))
2781  InsertPos = InstParent->getFirstInsertionPt();
2782 
2783  InstParent->getInstList().insert(InsertPos, Result);
2784 
2786  } else {
2787 #ifndef NDEBUG
2788  DEBUG(dbgs() << "IC: Mod = " << OrigI << '\n'
2789  << " New = " << *I << '\n');
2790 #endif
2791 
2792  // If the instruction was modified, it's possible that it is now dead.
2793  // if so, remove it.
2794  if (isInstructionTriviallyDead(I, TLI)) {
2796  } else {
2797  Worklist.Add(I);
2799  }
2800  }
2801  MadeIRChange = true;
2802  }
2803  }
2804 
2805  Worklist.Zap();
2806  return MadeIRChange;
2807 }
Constant * ConstantFoldInstruction(Instruction *I, const DataLayout *TD=nullptr, const TargetLibraryInfo *TLI=nullptr)
void SetCurrentDebugLocation(const DebugLoc &L)
Set location information used by debugging information.
Definition: IRBuilder.h:118
iterator end()
Definition: Function.h:404
size_type count(PtrType Ptr) const
count - Return 1 if the specified pointer is in the set, 0 otherwise.
Definition: SmallPtrSet.h:267
void Add(Instruction *I)
::std::string string
Definition: gtest-port.h:766
StringRef getName() const
Definition: Value.cpp:168
TargetLibraryInfo * TLI
Definition: InstCombine.h:90
bool isUnknown() const
isUnknown - Return true if this is an unknown location.
Definition: DebugLoc.h:71
A Use represents the edge between a Value definition and its users.
Definition: Use.h:69
void print(raw_ostream &O) const
Definition: AsmWriter.cpp:2239
Interval::succ_iterator succ_begin(Interval *I)
Definition: Interval.h:104
void visit(Iterator Start, Iterator End)
Definition: InstVisitor.h:90
void replaceAllUsesWith(Value *V)
Definition: Value.cpp:340
iterator begin()
Definition: Function.h:402
void SetInsertPoint(BasicBlock *TheBB)
This specifies that created instructions should be appended to the end of the specified block...
Definition: IRBuilder.h:83
Interval::succ_iterator succ_end(Interval *I)
Definition: Interval.h:107
LLVM Basic Block Representation.
Definition: BasicBlock.h:72
void AddUsersToWorkList(Instruction &I)
LLVM Constant Representation.
Definition: Constant.h:41
Instruction * ReplaceInstUsesWith(Instruction &I, Value *V)
Definition: InstCombine.h:282
const DebugLoc & getDebugLoc() const
getDebugLoc - Return the debug location for this node as a DebugLoc.
Definition: Instruction.h:196
const InstListType & getInstList() const
Return the underlying instruction list container.
Definition: BasicBlock.h:219
static bool AddReachableCodeToWorklist(BasicBlock *BB, SmallPtrSet< BasicBlock *, 64 > &Visited, InstCombiner &IC, const DataLayout *DL, const TargetLibraryInfo *TLI)
iterator insert(iterator where, NodeTy *New)
Definition: ilist.h:412
Value * getOperand(unsigned i) const
Definition: User.h:90
op_range operands()
Definition: User.h:124
InstCombineWorklist Worklist
Worklist - All of the instructions that need to be simplified.
Definition: InstCombine.h:97
#define DEBUG(X)
BuilderTy * Builder
Definition: InstCombine.h:102
static UndefValue * get(Type *T)
Definition: Constants.cpp:1372
Type * getType() const
Definition: Value.h:215
raw_ostream & dbgs()
dbgs - Return a circular-buffered debug stream.
static bool TryToSinkInstruction(Instruction *I, BasicBlock *DestBlock)
use_iterator use_begin()
Definition: Value.h:254
#define I(x, y, z)
Definition: MD5.cpp:54
bool hasOneUse() const
Definition: Value.h:284
bool use_empty() const
Definition: Value.h:250
const DataLayout * DL
Definition: InstCombine.h:89
user_iterator user_begin()
Definition: Value.h:267
bool isInstructionTriviallyDead(Instruction *I, const TargetLibraryInfo *TLI=nullptr)
Instruction * EraseInstFromFunction(Instruction &I)
Definition: InstCombine.h:301
iterator getFirstInsertionPt()
Returns an iterator to the first instruction in this block that is suitable for inserting a non-PHI i...
Definition: BasicBlock.cpp:174
const BasicBlock * getParent() const
Definition: Instruction.h:53
void Pass::dump ( ) const
inherited

Definition at line 116 of file Pass.cpp.

116  {
117  print(dbgs(), nullptr);
118 }
raw_ostream & dbgs()
dbgs - Return a circular-buffered debug stream.
virtual void print(raw_ostream &O, const Module *M) const
Definition: Pass.cpp:111
void Pass::dumpPassStructure ( unsigned  Offset = 0)
virtualinherited

Reimplemented in llvm::FPPassManager, llvm::LPPassManager, and llvm::RGPassManager.

Definition at line 53 of file Pass.cpp.

53  {
54  dbgs().indent(Offset*2) << getPassName() << "\n";
55 }
raw_ostream & indent(unsigned NumSpaces)
indent - Insert 'NumSpaces' spaces.
virtual const char * getPassName() const
Definition: Pass.cpp:61
raw_ostream & dbgs()
dbgs - Return a circular-buffered debug stream.
Value * InstCombiner::dyn_castFNegVal ( Value V,
bool  NoSignedZero = false 
) const
private

Definition at line 631 of file InstructionCombining.cpp.

631  {
632  if (BinaryOperator::isFNeg(V, IgnoreZeroSign))
634 
635  // Constants can be considered to be negated values if they can be folded.
636  if (ConstantFP *C = dyn_cast<ConstantFP>(V))
637  return ConstantExpr::getFNeg(C);
638 
639  if (ConstantDataVector *C = dyn_cast<ConstantDataVector>(V))
640  if (C->getType()->getElementType()->isFloatingPointTy())
641  return ConstantExpr::getFNeg(C);
642 
643  return nullptr;
644 }
static const Value * getFNegArgument(const Value *BinOp)
static Constant * getFNeg(Constant *C)
Definition: Constants.cpp:2090
static bool isFNeg(const Value *V, bool IgnoreZeroSign=false)
Value * InstCombiner::dyn_castNegVal ( Value V) const
private

Definition at line 612 of file InstructionCombining.cpp.

612  {
613  if (BinaryOperator::isNeg(V))
615 
616  // Constants can be considered to be negated values if they can be folded.
617  if (ConstantInt *C = dyn_cast<ConstantInt>(V))
618  return ConstantExpr::getNeg(C);
619 
620  if (ConstantDataVector *C = dyn_cast<ConstantDataVector>(V))
621  if (C->getType()->getElementType()->isIntegerTy())
622  return ConstantExpr::getNeg(C);
623 
624  return nullptr;
625 }
static const Value * getNegArgument(const Value *BinOp)
Class for constant integers.
Definition: Constants.h:51
static bool isNeg(const Value *V)
static Constant * getNeg(Constant *C, bool HasNUW=false, bool HasNSW=false)
Definition: Constants.cpp:2083
Value * InstCombiner::EmitGEPOffset ( User GEP)
private

Definition at line 98 of file InstructionCombining.cpp.

98  {
99  return llvm::EmitGEPOffset(Builder, *getDataLayout(), GEP);
100 }
Value * EmitGEPOffset(IRBuilderTy *Builder, const DataLayout &TD, User *GEP, bool NoAssumptions=false)
Definition: Local.h:187
BuilderTy * Builder
Definition: InstCombine.h:102
const DataLayout * getDataLayout() const
Definition: InstCombine.h:117
Instruction* llvm::InstCombiner::EraseInstFromFunction ( Instruction I)
inline

Definition at line 301 of file InstCombine.h.

301  {
302  DEBUG(dbgs() << "IC: ERASE " << I << '\n');
303 
304  assert(I.use_empty() && "Cannot erase instruction that is used!");
305  // Make sure that we reprocess all operands now that we reduced their
306  // use counts.
307  if (I.getNumOperands() < 8) {
308  for (User::op_iterator i = I.op_begin(), e = I.op_end(); i != e; ++i)
309  if (Instruction *Op = dyn_cast<Instruction>(*i))
310  Worklist.Add(Op);
311  }
312  Worklist.Remove(&I);
313  I.eraseFromParent();
314  MadeIRChange = true;
315  return nullptr; // Don't do anything with FI
316  }
void Remove(Instruction *I)
void Add(Instruction *I)
assert(Globals.size() > 1)
InstCombineWorklist Worklist
Worklist - All of the instructions that need to be simplified.
Definition: InstCombine.h:97
#define DEBUG(X)
raw_ostream & dbgs()
dbgs - Return a circular-buffered debug stream.
#define I(x, y, z)
Definition: MD5.cpp:54
Use * op_iterator
Definition: User.h:115
Value * InstCombiner::EvaluateInDifferentElementOrder ( Value V,
ArrayRef< int >  Mask 
)
private

Definition at line 744 of file InstCombineVectorOps.cpp.

744  {
745  // Mask.size() does not need to be equal to the number of vector elements.
746 
747  assert(V->getType()->isVectorTy() && "can't reorder non-vector elements");
748  if (isa<UndefValue>(V)) {
750  Mask.size()));
751  }
752  if (isa<ConstantAggregateZero>(V)) {
755  Mask.size()));
756  }
757  if (Constant *C = dyn_cast<Constant>(V)) {
758  SmallVector<Constant *, 16> MaskValues;
759  for (int i = 0, e = Mask.size(); i != e; ++i) {
760  if (Mask[i] == -1)
762  else
763  MaskValues.push_back(Builder->getInt32(Mask[i]));
764  }
766  ConstantVector::get(MaskValues));
767  }
768 
769  Instruction *I = cast<Instruction>(V);
770  switch (I->getOpcode()) {
771  case Instruction::Add:
772  case Instruction::FAdd:
773  case Instruction::Sub:
774  case Instruction::FSub:
775  case Instruction::Mul:
776  case Instruction::FMul:
777  case Instruction::UDiv:
778  case Instruction::SDiv:
779  case Instruction::FDiv:
780  case Instruction::URem:
781  case Instruction::SRem:
782  case Instruction::FRem:
783  case Instruction::Shl:
784  case Instruction::LShr:
785  case Instruction::AShr:
786  case Instruction::And:
787  case Instruction::Or:
788  case Instruction::Xor:
789  case Instruction::ICmp:
790  case Instruction::FCmp:
791  case Instruction::Trunc:
792  case Instruction::ZExt:
793  case Instruction::SExt:
794  case Instruction::FPToUI:
795  case Instruction::FPToSI:
796  case Instruction::UIToFP:
797  case Instruction::SIToFP:
798  case Instruction::FPTrunc:
799  case Instruction::FPExt:
800  case Instruction::Select:
801  case Instruction::GetElementPtr: {
802  SmallVector<Value*, 8> NewOps;
803  bool NeedsRebuild = (Mask.size() != I->getType()->getVectorNumElements());
804  for (int i = 0, e = I->getNumOperands(); i != e; ++i) {
806  NewOps.push_back(V);
807  NeedsRebuild |= (V != I->getOperand(i));
808  }
809  if (NeedsRebuild) {
810  return BuildNew(I, NewOps);
811  }
812  return I;
813  }
814  case Instruction::InsertElement: {
815  int Element = cast<ConstantInt>(I->getOperand(2))->getLimitedValue();
816 
817  // The insertelement was inserting at Element. Figure out which element
818  // that becomes after shuffling. The answer is guaranteed to be unique
819  // by CanEvaluateShuffled.
820  bool Found = false;
821  int Index = 0;
822  for (int e = Mask.size(); Index != e; ++Index) {
823  if (Mask[Index] == Element) {
824  Found = true;
825  break;
826  }
827  }
828 
829  // If element is not in Mask, no need to handle the operand 1 (element to
830  // be inserted). Just evaluate values in operand 0 according to Mask.
831  if (!Found)
832  return EvaluateInDifferentElementOrder(I->getOperand(0), Mask);
833 
835  return InsertElementInst::Create(V, I->getOperand(1),
836  Builder->getInt32(Index), "", I);
837  }
838  }
839  llvm_unreachable("failed to reorder elements of vector instruction!");
840 }
static Constant * getShuffleVector(Constant *V1, Constant *V2, Constant *Mask)
Definition: Constants.cpp:2022
unsigned getNumOperands() const
Definition: User.h:110
static ConstantAggregateZero * get(Type *Ty)
Definition: Constants.cpp:1269
IntegerType * getInt32Ty()
Fetch the type representing a 32-bit integer.
Definition: IRBuilder.h:316
static InsertElementInst * Create(Value *Vec, Value *NewElt, Value *Idx, const Twine &NameStr="", Instruction *InsertBefore=nullptr)
static Value * BuildNew(Instruction *I, ArrayRef< Value * > NewOps)
#define llvm_unreachable(msg)
Definition: ErrorHandling.h:98
static Constant * get(ArrayRef< Constant * > V)
Definition: Constants.cpp:961
size_t size() const
size - Get the array size.
Definition: ArrayRef.h:109
assert(Globals.size() > 1)
bool isVectorTy() const
Definition: Type.h:226
LLVM Constant Representation.
Definition: Constant.h:41
APInt Or(const APInt &LHS, const APInt &RHS)
Bitwise OR function for APInt.
Definition: APInt.h:1870
APInt Xor(const APInt &LHS, const APInt &RHS)
Bitwise XOR function for APInt.
Definition: APInt.h:1875
Value * getOperand(unsigned i) const
Definition: User.h:90
Value * EvaluateInDifferentElementOrder(Value *V, ArrayRef< int > Mask)
BuilderTy * Builder
Definition: InstCombine.h:102
static UndefValue * get(Type *T)
Definition: Constants.cpp:1372
unsigned getVectorNumElements() const
Definition: Type.cpp:208
Type * getType() const
Definition: Value.h:215
ConstantInt * getInt32(uint32_t C)
Get a constant 32-bit value.
Definition: IRBuilder.h:276
const Type * getScalarType() const LLVM_READONLY
Definition: Type.cpp:51
APInt And(const APInt &LHS, const APInt &RHS)
Bitwise AND function for APInt.
Definition: APInt.h:1865
#define I(x, y, z)
Definition: MD5.cpp:54
LLVM Value Representation.
Definition: Value.h:69
unsigned getOpcode() const
getOpcode() returns a member of one of the enums like Instruction::Add.
Definition: Instruction.h:86
static VectorType * get(Type *ElementType, unsigned NumElements)
Definition: Type.cpp:703
Value * InstCombiner::EvaluateInDifferentType ( Value V,
Type Ty,
bool  isSigned 
)
private

EvaluateInDifferentType - Given an expression that CanEvaluateTruncated or CanEvaluateSExtd returns true for, actually insert the code to evaluate the expression.

Definition at line 163 of file InstCombineCasts.cpp.

164  {
165  if (Constant *C = dyn_cast<Constant>(V)) {
166  C = ConstantExpr::getIntegerCast(C, Ty, isSigned /*Sext or ZExt*/);
167  // If we got a constantexpr back, try to simplify it with DL info.
168  if (ConstantExpr *CE = dyn_cast<ConstantExpr>(C))
170  return C;
171  }
172 
173  // Otherwise, it must be an instruction.
174  Instruction *I = cast<Instruction>(V);
175  Instruction *Res = nullptr;
176  unsigned Opc = I->getOpcode();
177  switch (Opc) {
178  case Instruction::Add:
179  case Instruction::Sub:
180  case Instruction::Mul:
181  case Instruction::And:
182  case Instruction::Or:
183  case Instruction::Xor:
184  case Instruction::AShr:
185  case Instruction::LShr:
186  case Instruction::Shl:
187  case Instruction::UDiv:
188  case Instruction::URem: {
189  Value *LHS = EvaluateInDifferentType(I->getOperand(0), Ty, isSigned);
190  Value *RHS = EvaluateInDifferentType(I->getOperand(1), Ty, isSigned);
191  Res = BinaryOperator::Create((Instruction::BinaryOps)Opc, LHS, RHS);
192  break;
193  }
194  case Instruction::Trunc:
195  case Instruction::ZExt:
196  case Instruction::SExt:
197  // If the source type of the cast is the type we're trying for then we can
198  // just return the source. There's no need to insert it because it is not
199  // new.
200  if (I->getOperand(0)->getType() == Ty)
201  return I->getOperand(0);
202 
203  // Otherwise, must be the same type of cast, so just reinsert a new one.
204  // This also handles the case of zext(trunc(x)) -> zext(x).
205  Res = CastInst::CreateIntegerCast(I->getOperand(0), Ty,
206  Opc == Instruction::SExt);
207  break;
208  case Instruction::Select: {
209  Value *True = EvaluateInDifferentType(I->getOperand(1), Ty, isSigned);
210  Value *False = EvaluateInDifferentType(I->getOperand(2), Ty, isSigned);
211  Res = SelectInst::Create(I->getOperand(0), True, False);
212  break;
213  }
214  case Instruction::PHI: {
215  PHINode *OPN = cast<PHINode>(I);
216  PHINode *NPN = PHINode::Create(Ty, OPN->getNumIncomingValues());
217  for (unsigned i = 0, e = OPN->getNumIncomingValues(); i != e; ++i) {
218  Value *V =EvaluateInDifferentType(OPN->getIncomingValue(i), Ty, isSigned);
219  NPN->addIncoming(V, OPN->getIncomingBlock(i));
220  }
221  Res = NPN;
222  break;
223  }
224  default:
225  // TODO: Can handle more cases here.
226  llvm_unreachable("Unreachable!");
227  }
228 
229  Res->takeName(I);
230  return InsertNewInstWith(Res, *I);
231 }
Instruction * InsertNewInstWith(Instruction *New, Instruction &Old)
Definition: InstCombine.h:271
TargetLibraryInfo * TLI
Definition: InstCombine.h:90
static Constant * getIntegerCast(Constant *C, Type *Ty, bool isSigned)
Create a ZExt, Bitcast or Trunc for integer -> integer casts.
Definition: Constants.cpp:1562
#define llvm_unreachable(msg)
Definition: ErrorHandling.h:98
static SelectInst * Create(Value *C, Value *S1, Value *S2, const Twine &NameStr="", Instruction *InsertBefore=nullptr)
unsigned getNumIncomingValues() const
Value * EvaluateInDifferentType(Value *V, Type *Ty, bool isSigned)
LLVM Constant Representation.
Definition: Constant.h:41
APInt Or(const APInt &LHS, const APInt &RHS)
Bitwise OR function for APInt.
Definition: APInt.h:1870
APInt Xor(const APInt &LHS, const APInt &RHS)
Bitwise XOR function for APInt.
Definition: APInt.h:1875
BasicBlock * getIncomingBlock(unsigned i) const
Value * getOperand(unsigned i) const
Definition: User.h:90
static CastInst * CreateIntegerCast(Value *S, Type *Ty, bool isSigned, const Twine &Name="", Instruction *InsertBefore=nullptr)
Create a ZExt, BitCast, or Trunc for int -> int casts.
Value * getIncomingValue(unsigned i) const
Type * getType() const
Definition: Value.h:215
static PHINode * Create(Type *Ty, unsigned NumReservedValues, const Twine &NameStr="", Instruction *InsertBefore=nullptr)
Constant * ConstantFoldConstantExpression(const ConstantExpr *CE, const DataLayout *TD=nullptr, const TargetLibraryInfo *TLI=nullptr)
static BinaryOperator * Create(BinaryOps Op, Value *S1, Value *S2, const Twine &Name=Twine(), Instruction *InsertBefore=nullptr)
APInt And(const APInt &LHS, const APInt &RHS)
Bitwise AND function for APInt.
Definition: APInt.h:1865
#define I(x, y, z)
Definition: MD5.cpp:54
const DataLayout * DL
Definition: InstCombine.h:89
LLVM Value Representation.
Definition: Value.h:69
unsigned getOpcode() const
getOpcode() returns a member of one of the enums like Instruction::Add.
Definition: Instruction.h:86
Type * InstCombiner::FindElementAtOffset ( Type PtrTy,
int64_t  Offset,
SmallVectorImpl< Value * > &  NewIndices 
)
private

FindElementAtOffset - Given a pointer type and a constant offset, determine whether or not there is a sequence of GEP indices into the pointed type that will land us at the specified offset. If so, fill them into NewIndices and return the resultant element type, otherwise return null.

Definition at line 865 of file InstructionCombining.cpp.

866  {
867  assert(PtrTy->isPtrOrPtrVectorTy());
868 
869  if (!DL)
870  return nullptr;
871 
872  Type *Ty = PtrTy->getPointerElementType();
873  if (!Ty->isSized())
874  return nullptr;
875 
876  // Start with the index over the outer type. Note that the type size
877  // might be zero (even if the offset isn't zero) if the indexed type
878  // is something like [0 x {int, int}]
879  Type *IntPtrTy = DL->getIntPtrType(PtrTy);
880  int64_t FirstIdx = 0;
881  if (int64_t TySize = DL->getTypeAllocSize(Ty)) {
882  FirstIdx = Offset/TySize;
883  Offset -= FirstIdx*TySize;
884 
885  // Handle hosts where % returns negative instead of values [0..TySize).
886  if (Offset < 0) {
887  --FirstIdx;
888  Offset += TySize;
889  assert(Offset >= 0);
890  }
891  assert((uint64_t)Offset < (uint64_t)TySize && "Out of range offset");
892  }
893 
894  NewIndices.push_back(ConstantInt::get(IntPtrTy, FirstIdx));
895 
896  // Index into the types. If we fail, set OrigBase to null.
897  while (Offset) {
898  // Indexing into tail padding between struct/array elements.
899  if (uint64_t(Offset*8) >= DL->getTypeSizeInBits(Ty))
900  return nullptr;
901 
902  if (StructType *STy = dyn_cast<StructType>(Ty)) {
903  const StructLayout *SL = DL->getStructLayout(STy);
904  assert(Offset < (int64_t)SL->getSizeInBytes() &&
905  "Offset must stay within the indexed type");
906 
907  unsigned Elt = SL->getElementContainingOffset(Offset);
909  Elt));
910 
911  Offset -= SL->getElementOffset(Elt);
912  Ty = STy->getElementType(Elt);
913  } else if (ArrayType *AT = dyn_cast<ArrayType>(Ty)) {
914  uint64_t EltSize = DL->getTypeAllocSize(AT->getElementType());
915  assert(EltSize && "Cannot index into a zero-sized array");
916  NewIndices.push_back(ConstantInt::get(IntPtrTy,Offset/EltSize));
917  Offset %= EltSize;
918  Ty = AT->getElementType();
919  } else {
920  // Otherwise, we can't index into the middle of this atomic type, bail.
921  return nullptr;
922  }
923  }
924 
925  return Ty;
926 }
bool isPtrOrPtrVectorTy() const
Definition: Type.h:222
Type * getPointerElementType() const
Definition: Type.h:363
const StructLayout * getStructLayout(StructType *Ty) const
Definition: DataLayout.cpp:498
LLVMContext & getContext() const
getContext - Return the LLVMContext in which this type was uniqued.
Definition: Type.h:125
uint64_t getElementOffset(unsigned Idx) const
Definition: DataLayout.h:505
assert(Globals.size() > 1)
IntegerType * getIntPtrType(LLVMContext &C, unsigned AddressSpace=0) const
Definition: DataLayout.cpp:703
uint64_t getTypeAllocSize(Type *Ty) const
Definition: DataLayout.h:368
uint64_t getSizeInBytes() const
Definition: DataLayout.h:488
unsigned getElementContainingOffset(uint64_t Offset) const
Definition: DataLayout.cpp:79
static Constant * get(Type *Ty, uint64_t V, bool isSigned=false)
Definition: Constants.cpp:528
bool isSized(SmallPtrSet< const Type *, 4 > *Visited=nullptr) const
Definition: Type.h:268
static IntegerType * getInt32Ty(LLVMContext &C)
Definition: Type.cpp:235
const DataLayout * DL
Definition: InstCombine.h:89
uint64_t getTypeSizeInBits(Type *Ty) const
Definition: DataLayout.h:522
Value * InstCombiner::FoldAndOfFCmps ( FCmpInst LHS,
FCmpInst RHS 
)

FoldAndOfFCmps - Optimize (fcmp)&(fcmp). NOTE: Unlike the rest of instcombine, this returns a Value which should already be inserted into the function.

Definition at line 1027 of file InstCombineAndOrXor.cpp.

1027  {
1028  if (LHS->getPredicate() == FCmpInst::FCMP_ORD &&
1029  RHS->getPredicate() == FCmpInst::FCMP_ORD) {
1030  if (LHS->getOperand(0)->getType() != RHS->getOperand(0)->getType())
1031  return nullptr;
1032 
1033  // (fcmp ord x, c) & (fcmp ord y, c) -> (fcmp ord x, y)
1034  if (ConstantFP *LHSC = dyn_cast<ConstantFP>(LHS->getOperand(1)))
1035  if (ConstantFP *RHSC = dyn_cast<ConstantFP>(RHS->getOperand(1))) {
1036  // If either of the constants are nans, then the whole thing returns
1037  // false.
1038  if (LHSC->getValueAPF().isNaN() || RHSC->getValueAPF().isNaN())
1039  return Builder->getFalse();
1040  return Builder->CreateFCmpORD(LHS->getOperand(0), RHS->getOperand(0));
1041  }
1042 
1043  // Handle vector zeros. This occurs because the canonical form of
1044  // "fcmp ord x,x" is "fcmp ord x, 0".
1045  if (isa<ConstantAggregateZero>(LHS->getOperand(1)) &&
1046  isa<ConstantAggregateZero>(RHS->getOperand(1)))
1047  return Builder->CreateFCmpORD(LHS->getOperand(0), RHS->getOperand(0));
1048  return nullptr;
1049  }
1050 
1051  Value *Op0LHS = LHS->getOperand(0), *Op0RHS = LHS->getOperand(1);
1052  Value *Op1LHS = RHS->getOperand(0), *Op1RHS = RHS->getOperand(1);
1053  FCmpInst::Predicate Op0CC = LHS->getPredicate(), Op1CC = RHS->getPredicate();
1054 
1055 
1056  if (Op0LHS == Op1RHS && Op0RHS == Op1LHS) {
1057  // Swap RHS operands to match LHS.
1058  Op1CC = FCmpInst::getSwappedPredicate(Op1CC);
1059  std::swap(Op1LHS, Op1RHS);
1060  }
1061 
1062  if (Op0LHS == Op1LHS && Op0RHS == Op1RHS) {
1063  // Simplify (fcmp cc0 x, y) & (fcmp cc1 x, y).
1064  if (Op0CC == Op1CC)
1065  return Builder->CreateFCmp((FCmpInst::Predicate)Op0CC, Op0LHS, Op0RHS);
1066  if (Op0CC == FCmpInst::FCMP_FALSE || Op1CC == FCmpInst::FCMP_FALSE)
1068  if (Op0CC == FCmpInst::FCMP_TRUE)
1069  return RHS;
1070  if (Op1CC == FCmpInst::FCMP_TRUE)
1071  return LHS;
1072 
1073  bool Op0Ordered;
1074  bool Op1Ordered;
1075  unsigned Op0Pred = getFCmpCode(Op0CC, Op0Ordered);
1076  unsigned Op1Pred = getFCmpCode(Op1CC, Op1Ordered);
1077  // uno && ord -> false
1078  if (Op0Pred == 0 && Op1Pred == 0 && Op0Ordered != Op1Ordered)
1080  if (Op1Pred == 0) {
1081  std::swap(LHS, RHS);
1082  std::swap(Op0Pred, Op1Pred);
1083  std::swap(Op0Ordered, Op1Ordered);
1084  }
1085  if (Op0Pred == 0) {
1086  // uno && ueq -> uno && (uno || eq) -> uno
1087  // ord && olt -> ord && (ord && lt) -> olt
1088  if (!Op0Ordered && (Op0Ordered == Op1Ordered))
1089  return LHS;
1090  if (Op0Ordered && (Op0Ordered == Op1Ordered))
1091  return RHS;
1092 
1093  // uno && oeq -> uno && (ord && eq) -> false
1094  if (!Op0Ordered)
1096  // ord && ueq -> ord && (uno || eq) -> oeq
1097  return getFCmpValue(true, Op1Pred, Op0LHS, Op0RHS, Builder);
1098  }
1099  }
1100 
1101  return nullptr;
1102 }
static Type * makeCmpResultType(Type *opnd_type)
Create a result type for fcmp/icmp.
Definition: InstrTypes.h:853
Value * CreateFCmp(CmpInst::Predicate P, Value *LHS, Value *RHS, const Twine &Name="")
Definition: IRBuilder.h:1328
Value * CreateFCmpORD(Value *LHS, Value *RHS, const Twine &Name="")
Definition: IRBuilder.h:1296
static Value * getFCmpValue(bool isordered, unsigned code, Value *LHS, Value *RHS, InstCombiner::BuilderTy *Builder)
static unsigned getFCmpCode(FCmpInst::Predicate CC, bool &isOrdered)
Value * getOperand(unsigned i) const
Definition: User.h:90
0 1 1 1 True if ordered (no nans)
Definition: InstrTypes.h:681
Predicate getPredicate() const
Return the predicate for this instruction.
Definition: InstrTypes.h:735
1 1 1 1 Always true (always folded)
Definition: InstrTypes.h:689
BuilderTy * Builder
Definition: InstCombine.h:102
Type * getType() const
Definition: Value.h:215
Predicate getSwappedPredicate() const
Return the predicate as if the operands were swapped.
Definition: InstrTypes.h:774
static Constant * get(Type *Ty, uint64_t V, bool isSigned=false)
Definition: Constants.cpp:528
void swap(llvm::BitVector &LHS, llvm::BitVector &RHS)
Implement std::swap in terms of BitVector swap.
Definition: BitVector.h:590
ConstantInt * getFalse()
Get the constant value for i1 false.
Definition: IRBuilder.h:261
LLVM Value Representation.
Definition: Value.h:69
0 0 0 0 Always false (always folded)
Definition: InstrTypes.h:674
Value * InstCombiner::FoldAndOfICmps ( ICmpInst LHS,
ICmpInst RHS 
)

FoldAndOfICmps - Fold (icmp)&(icmp) if possible.

Definition at line 791 of file InstCombineAndOrXor.cpp.

791  {
792  ICmpInst::Predicate LHSCC = LHS->getPredicate(), RHSCC = RHS->getPredicate();
793 
794  // (icmp1 A, B) & (icmp2 A, B) --> (icmp3 A, B)
795  if (PredicatesFoldable(LHSCC, RHSCC)) {
796  if (LHS->getOperand(0) == RHS->getOperand(1) &&
797  LHS->getOperand(1) == RHS->getOperand(0))
798  LHS->swapOperands();
799  if (LHS->getOperand(0) == RHS->getOperand(0) &&
800  LHS->getOperand(1) == RHS->getOperand(1)) {
801  Value *Op0 = LHS->getOperand(0), *Op1 = LHS->getOperand(1);
802  unsigned Code = getICmpCode(LHS) & getICmpCode(RHS);
803  bool isSigned = LHS->isSigned() || RHS->isSigned();
804  return getNewICmpValue(isSigned, Code, Op0, Op1, Builder);
805  }
806  }
807 
808  // handle (roughly): (icmp eq (A & B), C) & (icmp eq (A & D), E)
809  if (Value *V = foldLogOpOfMaskedICmps(LHS, RHS, true, Builder))
810  return V;
811 
812  // This only handles icmp of constants: (icmp1 A, C1) & (icmp2 B, C2).
813  Value *Val = LHS->getOperand(0), *Val2 = RHS->getOperand(0);
814  ConstantInt *LHSCst = dyn_cast<ConstantInt>(LHS->getOperand(1));
815  ConstantInt *RHSCst = dyn_cast<ConstantInt>(RHS->getOperand(1));
816  if (!LHSCst || !RHSCst) return nullptr;
817 
818  if (LHSCst == RHSCst && LHSCC == RHSCC) {
819  // (icmp ult A, C) & (icmp ult B, C) --> (icmp ult (A|B), C)
820  // where C is a power of 2
821  if (LHSCC == ICmpInst::ICMP_ULT &&
822  LHSCst->getValue().isPowerOf2()) {
823  Value *NewOr = Builder->CreateOr(Val, Val2);
824  return Builder->CreateICmp(LHSCC, NewOr, LHSCst);
825  }
826 
827  // (icmp eq A, 0) & (icmp eq B, 0) --> (icmp eq (A|B), 0)
828  if (LHSCC == ICmpInst::ICMP_EQ && LHSCst->isZero()) {
829  Value *NewOr = Builder->CreateOr(Val, Val2);
830  return Builder->CreateICmp(LHSCC, NewOr, LHSCst);
831  }
832  }
833 
834  // (trunc x) == C1 & (and x, CA) == C2 -> (and x, CA|CMAX) == C1|C2
835  // where CMAX is the all ones value for the truncated type,
836  // iff the lower bits of C2 and CA are zero.
837  if (LHSCC == ICmpInst::ICMP_EQ && LHSCC == RHSCC &&
838  LHS->hasOneUse() && RHS->hasOneUse()) {
839  Value *V;
840  ConstantInt *AndCst, *SmallCst = nullptr, *BigCst = nullptr;
841 
842  // (trunc x) == C1 & (and x, CA) == C2
843  // (and x, CA) == C2 & (trunc x) == C1
844  if (match(Val2, m_Trunc(m_Value(V))) &&
845  match(Val, m_And(m_Specific(V), m_ConstantInt(AndCst)))) {
846  SmallCst = RHSCst;
847  BigCst = LHSCst;
848  } else if (match(Val, m_Trunc(m_Value(V))) &&
849  match(Val2, m_And(m_Specific(V), m_ConstantInt(AndCst)))) {
850  SmallCst = LHSCst;
851  BigCst = RHSCst;
852  }
853 
854  if (SmallCst && BigCst) {
855  unsigned BigBitSize = BigCst->getType()->getBitWidth();
856  unsigned SmallBitSize = SmallCst->getType()->getBitWidth();
857 
858  // Check that the low bits are zero.
859  APInt Low = APInt::getLowBitsSet(BigBitSize, SmallBitSize);
860  if ((Low & AndCst->getValue()) == 0 && (Low & BigCst->getValue()) == 0) {
861  Value *NewAnd = Builder->CreateAnd(V, Low | AndCst->getValue());
862  APInt N = SmallCst->getValue().zext(BigBitSize) | BigCst->getValue();
863  Value *NewVal = ConstantInt::get(AndCst->getType()->getContext(), N);
864  return Builder->CreateICmp(LHSCC, NewAnd, NewVal);
865  }
866  }
867  }
868 
869  // From here on, we only handle:
870  // (icmp1 A, C1) & (icmp2 A, C2) --> something simpler.
871  if (Val != Val2) return nullptr;
872 
873  // ICMP_[US][GL]E X, CST is folded to ICMP_[US][GL]T elsewhere.
874  if (LHSCC == ICmpInst::ICMP_UGE || LHSCC == ICmpInst::ICMP_ULE ||
875  RHSCC == ICmpInst::ICMP_UGE || RHSCC == ICmpInst::ICMP_ULE ||
876  LHSCC == ICmpInst::ICMP_SGE || LHSCC == ICmpInst::ICMP_SLE ||
877  RHSCC == ICmpInst::ICMP_SGE || RHSCC == ICmpInst::ICMP_SLE)
878  return nullptr;
879 
880  // Make a constant range that's the intersection of the two icmp ranges.
881  // If the intersection is empty, we know that the result is false.
882  ConstantRange LHSRange =
883  ConstantRange::makeICmpRegion(LHSCC, LHSCst->getValue());
884  ConstantRange RHSRange =
885  ConstantRange::makeICmpRegion(RHSCC, RHSCst->getValue());
886 
887  if (LHSRange.intersectWith(RHSRange).isEmptySet())
889 
890  // We can't fold (ugt x, C) & (sgt x, C2).
891  if (!PredicatesFoldable(LHSCC, RHSCC))
892  return nullptr;
893 
894  // Ensure that the larger constant is on the RHS.
895  bool ShouldSwap;
896  if (CmpInst::isSigned(LHSCC) ||
897  (ICmpInst::isEquality(LHSCC) &&
898  CmpInst::isSigned(RHSCC)))
899  ShouldSwap = LHSCst->getValue().sgt(RHSCst->getValue());
900  else
901  ShouldSwap = LHSCst->getValue().ugt(RHSCst->getValue());
902 
903  if (ShouldSwap) {
904  std::swap(LHS, RHS);
905  std::swap(LHSCst, RHSCst);
906  std::swap(LHSCC, RHSCC);
907  }
908 
909  // At this point, we know we have two icmp instructions
910  // comparing a value against two constants and and'ing the result
911  // together. Because of the above check, we know that we only have
912  // icmp eq, icmp ne, icmp [su]lt, and icmp [SU]gt here. We also know
913  // (from the icmp folding check above), that the two constants
914  // are not equal and that the larger constant is on the RHS
915  assert(LHSCst != RHSCst && "Compares not folded above?");
916 
917  switch (LHSCC) {
918  default: llvm_unreachable("Unknown integer condition code!");
919  case ICmpInst::ICMP_EQ:
920  switch (RHSCC) {
921  default: llvm_unreachable("Unknown integer condition code!");
922  case ICmpInst::ICMP_NE: // (X == 13 & X != 15) -> X == 13
923  case ICmpInst::ICMP_ULT: // (X == 13 & X < 15) -> X == 13
924  case ICmpInst::ICMP_SLT: // (X == 13 & X < 15) -> X == 13
925  return LHS;
926  }
927  case ICmpInst::ICMP_NE:
928  switch (RHSCC) {
929  default: llvm_unreachable("Unknown integer condition code!");
930  case ICmpInst::ICMP_ULT:
931  if (LHSCst == SubOne(RHSCst)) // (X != 13 & X u< 14) -> X < 13
932  return Builder->CreateICmpULT(Val, LHSCst);
933  break; // (X != 13 & X u< 15) -> no change
934  case ICmpInst::ICMP_SLT:
935  if (LHSCst == SubOne(RHSCst)) // (X != 13 & X s< 14) -> X < 13
936  return Builder->CreateICmpSLT(Val, LHSCst);
937  break; // (X != 13 & X s< 15) -> no change
938  case ICmpInst::ICMP_EQ: // (X != 13 & X == 15) -> X == 15
939  case ICmpInst::ICMP_UGT: // (X != 13 & X u> 15) -> X u> 15
940  case ICmpInst::ICMP_SGT: // (X != 13 & X s> 15) -> X s> 15
941  return RHS;
942  case ICmpInst::ICMP_NE:
943  // Special case to get the ordering right when the values wrap around
944  // zero.
945  if (LHSCst->getValue() == 0 && RHSCst->getValue().isAllOnesValue())
946  std::swap(LHSCst, RHSCst);
947  if (LHSCst == SubOne(RHSCst)){// (X != 13 & X != 14) -> X-13 >u 1
948  Constant *AddCST = ConstantExpr::getNeg(LHSCst);
949  Value *Add = Builder->CreateAdd(Val, AddCST, Val->getName()+".off");
950  return Builder->CreateICmpUGT(Add, ConstantInt::get(Add->getType(), 1),
951  Val->getName()+".cmp");
952  }
953  break; // (X != 13 & X != 15) -> no change
954  }
955  break;
956  case ICmpInst::ICMP_ULT:
957  switch (RHSCC) {
958  default: llvm_unreachable("Unknown integer condition code!");
959  case ICmpInst::ICMP_EQ: // (X u< 13 & X == 15) -> false
960  case ICmpInst::ICMP_UGT: // (X u< 13 & X u> 15) -> false
962  case ICmpInst::ICMP_SGT: // (X u< 13 & X s> 15) -> no change
963  break;
964  case ICmpInst::ICMP_NE: // (X u< 13 & X != 15) -> X u< 13
965  case ICmpInst::ICMP_ULT: // (X u< 13 & X u< 15) -> X u< 13
966  return LHS;
967  case ICmpInst::ICMP_SLT: // (X u< 13 & X s< 15) -> no change
968  break;
969  }
970  break;
971  case ICmpInst::ICMP_SLT:
972  switch (RHSCC) {
973  default: llvm_unreachable("Unknown integer condition code!");
974  case ICmpInst::ICMP_UGT: // (X s< 13 & X u> 15) -> no change
975  break;
976  case ICmpInst::ICMP_NE: // (X s< 13 & X != 15) -> X < 13
977  case ICmpInst::ICMP_SLT: // (X s< 13 & X s< 15) -> X < 13
978  return LHS;
979  case ICmpInst::ICMP_ULT: // (X s< 13 & X u< 15) -> no change
980  break;
981  }
982  break;
983  case ICmpInst::ICMP_UGT:
984  switch (RHSCC) {
985  default: llvm_unreachable("Unknown integer condition code!");
986  case ICmpInst::ICMP_EQ: // (X u> 13 & X == 15) -> X == 15
987  case ICmpInst::ICMP_UGT: // (X u> 13 & X u> 15) -> X u> 15
988  return RHS;
989  case ICmpInst::ICMP_SGT: // (X u> 13 & X s> 15) -> no change
990  break;
991  case ICmpInst::ICMP_NE:
992  if (RHSCst == AddOne(LHSCst)) // (X u> 13 & X != 14) -> X u> 14
993  return Builder->CreateICmp(LHSCC, Val, RHSCst);
994  break; // (X u> 13 & X != 15) -> no change
995  case ICmpInst::ICMP_ULT: // (X u> 13 & X u< 15) -> (X-14) <u 1
996  return InsertRangeTest(Val, AddOne(LHSCst), RHSCst, false, true);
997  case ICmpInst::ICMP_SLT: // (X u> 13 & X s< 15) -> no change
998  break;
999  }
1000  break;
1001  case ICmpInst::ICMP_SGT:
1002  switch (RHSCC) {
1003  default: llvm_unreachable("Unknown integer condition code!");
1004  case ICmpInst::ICMP_EQ: // (X s> 13 & X == 15) -> X == 15
1005  case ICmpInst::ICMP_SGT: // (X s> 13 & X s> 15) -> X s> 15
1006  return RHS;
1007  case ICmpInst::ICMP_UGT: // (X s> 13 & X u> 15) -> no change
1008  break;
1009  case ICmpInst::ICMP_NE:
1010  if (RHSCst == AddOne(LHSCst)) // (X s> 13 & X != 14) -> X s> 14
1011  return Builder->CreateICmp(LHSCC, Val, RHSCst);
1012  break; // (X s> 13 & X != 15) -> no change
1013  case ICmpInst::ICMP_SLT: // (X s> 13 & X s< 15) -> (X-14) s< 1
1014  return InsertRangeTest(Val, AddOne(LHSCst), RHSCst, true, true);
1015  case ICmpInst::ICMP_ULT: // (X s> 13 & X u< 15) -> no change
1016  break;
1017  }
1018  break;
1019  }
1020 
1021  return nullptr;
1022 }
BinaryOp_match< LHS, RHS, Instruction::And > m_And(const LHS &L, const RHS &R)
Definition: PatternMatch.h:467
IntegerType * getType() const
Definition: Constants.h:139
class_match< Value > m_Value()
m_Value() - Match an arbitrary value and ignore it.
Definition: PatternMatch.h:70
Value * CreateICmp(CmpInst::Predicate P, Value *LHS, Value *RHS, const Twine &Name="")
Definition: IRBuilder.h:1321
static Type * makeCmpResultType(Type *opnd_type)
Create a result type for fcmp/icmp.
Definition: InstrTypes.h:853
Value * CreateICmpSLT(Value *LHS, Value *RHS, const Twine &Name="")
Definition: IRBuilder.h:1271
static Value * foldLogOpOfMaskedICmps(ICmpInst *LHS, ICmpInst *RHS, bool IsAnd, llvm::InstCombiner::BuilderTy *Builder)
unsigned getICmpCode(const ICmpInst *ICI, bool InvertPred=false)
static APInt getLowBitsSet(unsigned numBits, unsigned loBitsSet)
Get a value with low bits set.
Definition: APInt.h:526
unsigned less or equal
Definition: InstrTypes.h:698
unsigned less than
Definition: InstrTypes.h:697
bool isSigned() const
Determine if this instruction is using a signed comparison.
Definition: InstrTypes.h:801
Value * CreateICmpULT(Value *LHS, Value *RHS, const Twine &Name="")
Definition: IRBuilder.h:1259
unsigned getBitWidth() const
Get the number of bits in this IntegerType.
Definition: DerivedTypes.h:61
StringRef getName() const
Definition: Value.cpp:168
bool match(Val *V, const Pattern &P)
Definition: PatternMatch.h:42
const APInt & getValue() const
Return the constant's value.
Definition: Constants.h:105
#define llvm_unreachable(msg)
Definition: ErrorHandling.h:98
CastClass_match< OpTy, Instruction::Trunc > m_Trunc(const OpTy &Op)
m_Trunc
Definition: PatternMatch.h:768
static Constant * AddOne(Constant *C)
AddOne - Add one to a Constant.
Definition: InstCombine.h:60
Value * CreateAnd(Value *LHS, Value *RHS, const Twine &Name="")
Definition: IRBuilder.h:806
LLVMContext & getContext() const
getContext - Return the LLVMContext in which this type was uniqued.
Definition: Type.h:125
Value * CreateOr(Value *LHS, Value *RHS, const Twine &Name="")
Definition: IRBuilder.h:822
bool sgt(const APInt &RHS) const
Signed greather than comparison.
Definition: APInt.h:1096
class_match< ConstantInt > m_ConstantInt()
m_ConstantInt() - Match an arbitrary ConstantInt and ignore it.
Definition: PatternMatch.h:72
assert(Globals.size() > 1)
ConstantRange intersectWith(const ConstantRange &CR) const
Value * CreateAdd(Value *LHS, Value *RHS, const Twine &Name="", bool HasNUW=false, bool HasNSW=false)
Definition: IRBuilder.h:632
bool isEquality() const
LLVM Constant Representation.
Definition: Constant.h:41
specificval_ty m_Specific(const Value *V)
m_Specific - Match if we have a specific specified value.
Definition: PatternMatch.h:323
Value * InsertRangeTest(Value *V, Constant *Lo, Constant *Hi, bool isSigned, bool Inside)
Value * getOperand(unsigned i) const
Definition: User.h:90
Predicate getPredicate() const
Return the predicate for this instruction.
Definition: InstrTypes.h:735
BuilderTy * Builder
Definition: InstCombine.h:102
bool isEmptySet() const
void swapOperands()
Swap operands and adjust predicate.
bool isPowerOf2() const
Check if this APInt's value is a power of two greater than zero.
Definition: APInt.h:388
signed greater than
Definition: InstrTypes.h:699
bool ugt(const APInt &RHS) const
Unsigned greather than comparison.
Definition: APInt.h:1080
Class for constant integers.
Definition: Constants.h:51
Type * getType() const
Definition: Value.h:215
signed less than
Definition: InstrTypes.h:701
static Constant * get(Type *Ty, uint64_t V, bool isSigned=false)
Definition: Constants.cpp:528
bool isZero() const
Definition: Constants.h:160
bool PredicatesFoldable(CmpInst::Predicate p1, CmpInst::Predicate p2)
void swap(llvm::BitVector &LHS, llvm::BitVector &RHS)
Implement std::swap in terms of BitVector swap.
Definition: BitVector.h:590
signed less or equal
Definition: InstrTypes.h:702
Class for arbitrary precision integers.
Definition: APInt.h:75
LLVM_ATTRIBUTE_UNUSED_RESULT std::enable_if< !is_simple_type< Y >::value, typename cast_retty< X, const Y >::ret_type >::type dyn_cast(const Y &Val)
Definition: Casting.h:265
static Constant * getNeg(Constant *C, bool HasNUW=false, bool HasNSW=false)
Definition: Constants.cpp:2083
bool isAllOnesValue() const
Determine if all bits are set.
Definition: APInt.h:338
unsigned greater or equal
Definition: InstrTypes.h:696
static ConstantRange makeICmpRegion(unsigned Pred, const ConstantRange &Other)
#define N
bool hasOneUse() const
Definition: Value.h:284
LLVM Value Representation.
Definition: Value.h:69
static Value * getNewICmpValue(bool Sign, unsigned Code, Value *LHS, Value *RHS, InstCombiner::BuilderTy *Builder)
unsigned greater than
Definition: InstrTypes.h:695
APInt LLVM_ATTRIBUTE_UNUSED_RESULT zext(unsigned width) const
Zero extend to a new width.
Definition: APInt.cpp:984
Value * CreateICmpUGT(Value *LHS, Value *RHS, const Twine &Name="")
Definition: IRBuilder.h:1253
static Constant * SubOne(Constant *C)
SubOne - Subtract one from a Constant.
Definition: InstCombine.h:64
signed greater or equal
Definition: InstrTypes.h:700
Instruction * InstCombiner::FoldCmpLoadFromIndexedGlobal ( GetElementPtrInst GEP,
GlobalVariable GV,
CmpInst ICI,
ConstantInt AndCst = nullptr 
)

FoldCmpLoadFromIndexedGlobal - Called we see this pattern: cmp pred (load (gep GV, ...)), cmpcst where GV is a global variable with a constant initializer. Try to simplify this into some simple computation that does not need the load. For example we can optimize "icmp eq (load (gep "foo", 0, i)), 0" into "icmp eq i, 3".

If AndCst is non-null, then the loaded value is masked with that constant before doing the comparison. This handles cases like "A[i]&4 == 0".

TrueRangeEnd/FalseRangeEnd - In conjunction with First*Element, these define a state machine that triggers for ranges of values that the index is true or false for. This triggers on things like "abbbbc"[i] == 'b'. This is -2 when undefined, -3 when overdefined, and otherwise the last index in the range (inclusive). We use -2 for undefined here because we use relative comparisons and don't want 0-1 to match -1.

Definition at line 220 of file InstCombineCompares.cpp.

221  {
222  // We need TD information to know the pointer size unless this is inbounds.
223  if (!GEP->isInBounds() && !DL)
224  return nullptr;
225 
226  Constant *Init = GV->getInitializer();
227  if (!isa<ConstantArray>(Init) && !isa<ConstantDataArray>(Init))
228  return nullptr;
229 
230  uint64_t ArrayElementCount = Init->getType()->getArrayNumElements();
231  if (ArrayElementCount > 1024) return nullptr; // Don't blow up on huge arrays.
232 
233  // There are many forms of this optimization we can handle, for now, just do
234  // the simple index into a single-dimensional array.
235  //
236  // Require: GEP GV, 0, i {{, constant indices}}
237  if (GEP->getNumOperands() < 3 ||
238  !isa<ConstantInt>(GEP->getOperand(1)) ||
239  !cast<ConstantInt>(GEP->getOperand(1))->isZero() ||
240  isa<Constant>(GEP->getOperand(2)))
241  return nullptr;
242 
243  // Check that indices after the variable are constants and in-range for the
244  // type they index. Collect the indices. This is typically for arrays of
245  // structs.
246  SmallVector<unsigned, 4> LaterIndices;
247 
248  Type *EltTy = Init->getType()->getArrayElementType();
249  for (unsigned i = 3, e = GEP->getNumOperands(); i != e; ++i) {
250  ConstantInt *Idx = dyn_cast<ConstantInt>(GEP->getOperand(i));
251  if (!Idx) return nullptr; // Variable index.
252 
253  uint64_t IdxVal = Idx->getZExtValue();
254  if ((unsigned)IdxVal != IdxVal) return nullptr; // Too large array index.
255 
256  if (StructType *STy = dyn_cast<StructType>(EltTy))
257  EltTy = STy->getElementType(IdxVal);
258  else if (ArrayType *ATy = dyn_cast<ArrayType>(EltTy)) {
259  if (IdxVal >= ATy->getNumElements()) return nullptr;
260  EltTy = ATy->getElementType();
261  } else {
262  return nullptr; // Unknown type.
263  }
264 
265  LaterIndices.push_back(IdxVal);
266  }
267 
268  enum { Overdefined = -3, Undefined = -2 };
269 
270  // Variables for our state machines.
271 
272  // FirstTrueElement/SecondTrueElement - Used to emit a comparison of the form
273  // "i == 47 | i == 87", where 47 is the first index the condition is true for,
274  // and 87 is the second (and last) index. FirstTrueElement is -2 when
275  // undefined, otherwise set to the first true element. SecondTrueElement is
276  // -2 when undefined, -3 when overdefined and >= 0 when that index is true.
277  int FirstTrueElement = Undefined, SecondTrueElement = Undefined;
278 
279  // FirstFalseElement/SecondFalseElement - Used to emit a comparison of the
280  // form "i != 47 & i != 87". Same state transitions as for true elements.
281  int FirstFalseElement = Undefined, SecondFalseElement = Undefined;
282 
283  /// TrueRangeEnd/FalseRangeEnd - In conjunction with First*Element, these
284  /// define a state machine that triggers for ranges of values that the index
285  /// is true or false for. This triggers on things like "abbbbc"[i] == 'b'.
286  /// This is -2 when undefined, -3 when overdefined, and otherwise the last
287  /// index in the range (inclusive). We use -2 for undefined here because we
288  /// use relative comparisons and don't want 0-1 to match -1.
289  int TrueRangeEnd = Undefined, FalseRangeEnd = Undefined;
290 
291  // MagicBitvector - This is a magic bitvector where we set a bit if the
292  // comparison is true for element 'i'. If there are 64 elements or less in
293  // the array, this will fully represent all the comparison results.
294  uint64_t MagicBitvector = 0;
295 
296 
297  // Scan the array and see if one of our patterns matches.
298  Constant *CompareRHS = cast<Constant>(ICI.getOperand(1));
299  for (unsigned i = 0, e = ArrayElementCount; i != e; ++i) {
300  Constant *Elt = Init->getAggregateElement(i);
301  if (!Elt) return nullptr;
302 
303  // If this is indexing an array of structures, get the structure element.
304  if (!LaterIndices.empty())
305  Elt = ConstantExpr::getExtractValue(Elt, LaterIndices);
306 
307  // If the element is masked, handle it.
308  if (AndCst) Elt = ConstantExpr::getAnd(Elt, AndCst);
309 
310  // Find out if the comparison would be true or false for the i'th element.
312  CompareRHS, DL, TLI);
313  // If the result is undef for this element, ignore it.
314  if (isa<UndefValue>(C)) {
315  // Extend range state machines to cover this element in case there is an
316  // undef in the middle of the range.
317  if (TrueRangeEnd == (int)i-1)
318  TrueRangeEnd = i;
319  if (FalseRangeEnd == (int)i-1)
320  FalseRangeEnd = i;
321  continue;
322  }
323 
324  // If we can't compute the result for any of the elements, we have to give
325  // up evaluating the entire conditional.
326  if (!isa<ConstantInt>(C)) return nullptr;
327 
328  // Otherwise, we know if the comparison is true or false for this element,
329  // update our state machines.
330  bool IsTrueForElt = !cast<ConstantInt>(C)->isZero();
331 
332  // State machine for single/double/range index comparison.
333  if (IsTrueForElt) {
334  // Update the TrueElement state machine.
335  if (FirstTrueElement == Undefined)
336  FirstTrueElement = TrueRangeEnd = i; // First true element.
337  else {
338  // Update double-compare state machine.
339  if (SecondTrueElement == Undefined)
340  SecondTrueElement = i;
341  else
342  SecondTrueElement = Overdefined;
343 
344  // Update range state machine.
345  if (TrueRangeEnd == (int)i-1)
346  TrueRangeEnd = i;
347  else
348  TrueRangeEnd = Overdefined;
349  }
350  } else {
351  // Update the FalseElement state machine.
352  if (FirstFalseElement == Undefined)
353  FirstFalseElement = FalseRangeEnd = i; // First false element.
354  else {
355  // Update double-compare state machine.
356  if (SecondFalseElement == Undefined)
357  SecondFalseElement = i;
358  else
359  SecondFalseElement = Overdefined;
360 
361  // Update range state machine.
362  if (FalseRangeEnd == (int)i-1)
363  FalseRangeEnd = i;
364  else
365  FalseRangeEnd = Overdefined;
366  }
367  }
368 
369 
370  // If this element is in range, update our magic bitvector.
371  if (i < 64 && IsTrueForElt)
372  MagicBitvector |= 1ULL << i;
373 
374  // If all of our states become overdefined, bail out early. Since the
375  // predicate is expensive, only check it every 8 elements. This is only
376  // really useful for really huge arrays.
377  if ((i & 8) == 0 && i >= 64 && SecondTrueElement == Overdefined &&
378  SecondFalseElement == Overdefined && TrueRangeEnd == Overdefined &&
379  FalseRangeEnd == Overdefined)
380  return nullptr;
381  }
382 
383  // Now that we've scanned the entire array, emit our new comparison(s). We
384  // order the state machines in complexity of the generated code.
385  Value *Idx = GEP->getOperand(2);
386 
387  // If the index is larger than the pointer size of the target, truncate the
388  // index down like the GEP would do implicitly. We don't have to do this for
389  // an inbounds GEP because the index can't be out of range.
390  if (!GEP->isInBounds()) {
391  Type *IntPtrTy = DL->getIntPtrType(GEP->getType());
392  unsigned PtrSize = IntPtrTy->getIntegerBitWidth();
393  if (Idx->getType()->getPrimitiveSizeInBits() > PtrSize)
394  Idx = Builder->CreateTrunc(Idx, IntPtrTy);
395  }
396 
397  // If the comparison is only true for one or two elements, emit direct
398  // comparisons.
399  if (SecondTrueElement != Overdefined) {
400  // None true -> false.
401  if (FirstTrueElement == Undefined)
402  return ReplaceInstUsesWith(ICI, Builder->getFalse());
403 
404  Value *FirstTrueIdx = ConstantInt::get(Idx->getType(), FirstTrueElement);
405 
406  // True for one element -> 'i == 47'.
407  if (SecondTrueElement == Undefined)
408  return new ICmpInst(ICmpInst::ICMP_EQ, Idx, FirstTrueIdx);
409 
410  // True for two elements -> 'i == 47 | i == 72'.
411  Value *C1 = Builder->CreateICmpEQ(Idx, FirstTrueIdx);
412  Value *SecondTrueIdx = ConstantInt::get(Idx->getType(), SecondTrueElement);
413  Value *C2 = Builder->CreateICmpEQ(Idx, SecondTrueIdx);
414  return BinaryOperator::CreateOr(C1, C2);
415  }
416 
417  // If the comparison is only false for one or two elements, emit direct
418  // comparisons.
419  if (SecondFalseElement != Overdefined) {
420  // None false -> true.
421  if (FirstFalseElement == Undefined)
422  return ReplaceInstUsesWith(ICI, Builder->getTrue());
423 
424  Value *FirstFalseIdx = ConstantInt::get(Idx->getType(), FirstFalseElement);
425 
426  // False for one element -> 'i != 47'.
427  if (SecondFalseElement == Undefined)
428  return new ICmpInst(ICmpInst::ICMP_NE, Idx, FirstFalseIdx);
429 
430  // False for two elements -> 'i != 47 & i != 72'.
431  Value *C1 = Builder->CreateICmpNE(Idx, FirstFalseIdx);
432  Value *SecondFalseIdx = ConstantInt::get(Idx->getType(),SecondFalseElement);
433  Value *C2 = Builder->CreateICmpNE(Idx, SecondFalseIdx);
434  return BinaryOperator::CreateAnd(C1, C2);
435  }
436 
437  // If the comparison can be replaced with a range comparison for the elements
438  // where it is true, emit the range check.
439  if (TrueRangeEnd != Overdefined) {
440  assert(TrueRangeEnd != FirstTrueElement && "Should emit single compare");
441 
442  // Generate (i-FirstTrue) <u (TrueRangeEnd-FirstTrue+1).
443  if (FirstTrueElement) {
444  Value *Offs = ConstantInt::get(Idx->getType(), -FirstTrueElement);
445  Idx = Builder->CreateAdd(Idx, Offs);
446  }
447 
448  Value *End = ConstantInt::get(Idx->getType(),
449  TrueRangeEnd-FirstTrueElement+1);
450  return new ICmpInst(ICmpInst::ICMP_ULT, Idx, End);
451  }
452 
453  // False range check.
454  if (FalseRangeEnd != Overdefined) {
455  assert(FalseRangeEnd != FirstFalseElement && "Should emit single compare");
456  // Generate (i-FirstFalse) >u (FalseRangeEnd-FirstFalse).
457  if (FirstFalseElement) {
458  Value *Offs = ConstantInt::get(Idx->getType(), -FirstFalseElement);
459  Idx = Builder->CreateAdd(Idx, Offs);
460  }
461 
462  Value *End = ConstantInt::get(Idx->getType(),
463  FalseRangeEnd-FirstFalseElement);
464  return new ICmpInst(ICmpInst::ICMP_UGT, Idx, End);
465  }
466 
467 
468  // If a magic bitvector captures the entire comparison state
469  // of this load, replace it with computation that does:
470  // ((magic_cst >> i) & 1) != 0
471  {
472  Type *Ty = nullptr;
473 
474  // Look for an appropriate type:
475  // - The type of Idx if the magic fits
476  // - The smallest fitting legal type if we have a DataLayout
477  // - Default to i32
478  if (ArrayElementCount <= Idx->getType()->getIntegerBitWidth())
479  Ty = Idx->getType();
480  else if (DL)
481  Ty = DL->getSmallestLegalIntType(Init->getContext(), ArrayElementCount);
482  else if (ArrayElementCount <= 32)
483  Ty = Type::getInt32Ty(Init->getContext());
484 
485  if (Ty) {
486  Value *V = Builder->CreateIntCast(Idx, Ty, false);
487  V = Builder->CreateLShr(ConstantInt::get(Ty, MagicBitvector), V);
488  V = Builder->CreateAnd(ConstantInt::get(Ty, 1), V);
489  return new ICmpInst(ICmpInst::ICMP_NE, V, ConstantInt::get(Ty, 0));
490  }
491  }
492 
493  return nullptr;
494 }
Value * CreateLShr(Value *LHS, Value *RHS, const Twine &Name="", bool isExact=false)
Definition: IRBuilder.h:770
void push_back(const T &Elt)
Definition: SmallVector.h:225
static bool isZero(Value *V, const DataLayout *DL)
Definition: Lint.cpp:507
Value * CreateIntCast(Value *V, Type *DestTy, bool isSigned, const Twine &Name="")
Definition: IRBuilder.h:1221
unsigned getNumOperands() const
Definition: User.h:110
unsigned less than
Definition: InstrTypes.h:697
FunctionType * getType(LLVMContext &Context, ID id, ArrayRef< Type * > Tys=None)
Definition: Function.cpp:711
Value * CreateICmpEQ(Value *LHS, Value *RHS, const Twine &Name="")
Definition: IRBuilder.h:1247
const Constant * getInitializer() const
TargetLibraryInfo * TLI
Definition: InstCombine.h:90
Type * getArrayElementType() const
Definition: Type.h:358
static Constant * getExtractValue(Constant *Agg, ArrayRef< unsigned > Idxs)
Definition: Constants.cpp:2062
uint64_t getZExtValue() const
Return the zero extended value.
Definition: Constants.h:116
Value * CreateAnd(Value *LHS, Value *RHS, const Twine &Name="")
Definition: IRBuilder.h:806
bool LLVM_ATTRIBUTE_UNUSED_RESULT empty() const
Definition: SmallVector.h:57
bool isInBounds() const
isInBounds - Determine whether the GEP has the inbounds flag.
assert(Globals.size() > 1)
ConstantInt * getTrue()
Get the constant value for i1 true.
Definition: IRBuilder.h:256
Value * CreateAdd(Value *LHS, Value *RHS, const Twine &Name="", bool HasNUW=false, bool HasNSW=false)
Definition: IRBuilder.h:632
LLVM Constant Representation.
Definition: Constant.h:41
Instruction * ReplaceInstUsesWith(Instruction &I, Value *V)
Definition: InstCombine.h:282
static Constant * getAnd(Constant *C1, Constant *C2)
Definition: Constants.cpp:2161
Represent an integer comparison operator.
Definition: Instructions.h:977
Value * getOperand(unsigned i) const
Definition: User.h:90
Predicate getPredicate() const
Return the predicate for this instruction.
Definition: InstrTypes.h:735
Constant * getAggregateElement(unsigned Elt) const
Definition: Constants.cpp:205
BuilderTy * Builder
Definition: InstCombine.h:102
LLVMContext & getContext() const
All values hold a context through their type.
Definition: Value.cpp:615
unsigned getIntegerBitWidth() const
Definition: Type.cpp:172
Class for constant integers.
Definition: Constants.h:51
Type * getType() const
Definition: Value.h:215
SequentialType * getType() const
Definition: Instructions.h:830
static Constant * get(Type *Ty, uint64_t V, bool isSigned=false)
Definition: Constants.cpp:528
LLVM_ATTRIBUTE_UNUSED_RESULT std::enable_if< !is_simple_type< Y >::value, typename cast_retty< X, const Y >::ret_type >::type dyn_cast(const Y &Val)
Definition: Casting.h:265
ConstantInt * getFalse()
Get the constant value for i1 false.
Definition: IRBuilder.h:261
static IntegerType * getInt32Ty(LLVMContext &C)
Definition: Type.cpp:235
Value * CreateICmpNE(Value *LHS, Value *RHS, const Twine &Name="")
Definition: IRBuilder.h:1250
uint64_t getArrayNumElements() const
Definition: Type.cpp:204
const DataLayout * DL
Definition: InstCombine.h:89
unsigned getPrimitiveSizeInBits() const LLVM_READONLY
Definition: Type.cpp:117
Value * CreateTrunc(Value *V, Type *DestTy, const Twine &Name="")
Definition: IRBuilder.h:1094
LLVM Value Representation.
Definition: Value.h:69
unsigned greater than
Definition: InstrTypes.h:695
Constant * ConstantFoldCompareInstOperands(unsigned Predicate, Constant *LHS, Constant *RHS, const DataLayout *TD=nullptr, const TargetLibraryInfo *TLI=nullptr)
Instruction * InstCombiner::FoldFCmp_IntToFP_Cst ( FCmpInst I,
Instruction LHSI,
Constant RHSC 
)

FoldFCmp_IntToFP_Cst - Fold fcmp ([us]itofp x, cst) if possible.

Definition at line 3291 of file InstCombineCompares.cpp.

3293  {
3294  if (!isa<ConstantFP>(RHSC)) return nullptr;
3295  const APFloat &RHS = cast<ConstantFP>(RHSC)->getValueAPF();
3296 
3297  // Get the width of the mantissa. We don't want to hack on conversions that
3298  // might lose information from the integer, e.g. "i64 -> float"
3299  int MantissaWidth = LHSI->getType()->getFPMantissaWidth();
3300  if (MantissaWidth == -1) return nullptr; // Unknown.
3301 
3302  // Check to see that the input is converted from an integer type that is small
3303  // enough that preserves all bits. TODO: check here for "known" sign bits.
3304  // This would allow us to handle (fptosi (x >>s 62) to float) if x is i64 f.e.
3305  unsigned InputSize = LHSI->getOperand(0)->getType()->getScalarSizeInBits();
3306 
3307  // If this is a uitofp instruction, we need an extra bit to hold the sign.
3308  bool LHSUnsigned = isa<UIToFPInst>(LHSI);
3309  if (LHSUnsigned)
3310  ++InputSize;
3311 
3312  // If the conversion would lose info, don't hack on this.
3313  if ((int)InputSize > MantissaWidth)
3314  return nullptr;
3315 
3316  // Otherwise, we can potentially simplify the comparison. We know that it
3317  // will always come through as an integer value and we know the constant is
3318  // not a NAN (it would have been previously simplified).
3319  assert(!RHS.isNaN() && "NaN comparison not already folded!");
3320 
3321  ICmpInst::Predicate Pred;
3322  switch (I.getPredicate()) {
3323  default: llvm_unreachable("Unexpected predicate!");
3324  case FCmpInst::FCMP_UEQ:
3325  case FCmpInst::FCMP_OEQ:
3326  Pred = ICmpInst::ICMP_EQ;
3327  break;
3328  case FCmpInst::FCMP_UGT:
3329  case FCmpInst::FCMP_OGT:
3330  Pred = LHSUnsigned ? ICmpInst::ICMP_UGT : ICmpInst::ICMP_SGT;
3331  break;
3332  case FCmpInst::FCMP_UGE:
3333  case FCmpInst::FCMP_OGE:
3334  Pred = LHSUnsigned ? ICmpInst::ICMP_UGE : ICmpInst::ICMP_SGE;
3335  break;
3336  case FCmpInst::FCMP_ULT:
3337  case FCmpInst::FCMP_OLT:
3338  Pred = LHSUnsigned ? ICmpInst::ICMP_ULT : ICmpInst::ICMP_SLT;
3339  break;
3340  case FCmpInst::FCMP_ULE:
3341  case FCmpInst::FCMP_OLE:
3342  Pred = LHSUnsigned ? ICmpInst::ICMP_ULE : ICmpInst::ICMP_SLE;
3343  break;
3344  case FCmpInst::FCMP_UNE:
3345  case FCmpInst::FCMP_ONE:
3346  Pred = ICmpInst::ICMP_NE;
3347  break;
3348  case FCmpInst::FCMP_ORD:
3349  return ReplaceInstUsesWith(I, Builder->getTrue());
3350  case FCmpInst::FCMP_UNO:
3351  return ReplaceInstUsesWith(I, Builder->getFalse());
3352  }
3353 
3354  IntegerType *IntTy = cast<IntegerType>(LHSI->getOperand(0)->getType());
3355 
3356  // Now we know that the APFloat is a normal number, zero or inf.
3357 
3358  // See if the FP constant is too large for the integer. For example,
3359  // comparing an i8 to 300.0.
3360  unsigned IntWidth = IntTy->getScalarSizeInBits();
3361 
3362  if (!LHSUnsigned) {
3363  // If the RHS value is > SignedMax, fold the comparison. This handles +INF
3364  // and large values.
3365  APFloat SMax(RHS.getSemantics());
3366  SMax.convertFromAPInt(APInt::getSignedMaxValue(IntWidth), true,
3368  if (SMax.compare(RHS) == APFloat::cmpLessThan) { // smax < 13123.0
3369  if (Pred == ICmpInst::ICMP_NE || Pred == ICmpInst::ICMP_SLT ||
3370  Pred == ICmpInst::ICMP_SLE)
3371  return ReplaceInstUsesWith(I, Builder->getTrue());
3372  return ReplaceInstUsesWith(I, Builder->getFalse());
3373  }
3374  } else {
3375  // If the RHS value is > UnsignedMax, fold the comparison. This handles
3376  // +INF and large values.
3377  APFloat UMax(RHS.getSemantics());
3378  UMax.convertFromAPInt(APInt::getMaxValue(IntWidth), false,
3380  if (UMax.compare(RHS) == APFloat::cmpLessThan) { // umax < 13123.0
3381  if (Pred == ICmpInst::ICMP_NE || Pred == ICmpInst::ICMP_ULT ||
3382  Pred == ICmpInst::ICMP_ULE)
3383  return ReplaceInstUsesWith(I, Builder->getTrue());
3384  return ReplaceInstUsesWith(I, Builder->getFalse());
3385  }
3386  }
3387 
3388  if (!LHSUnsigned) {
3389  // See if the RHS value is < SignedMin.
3390  APFloat SMin(RHS.getSemantics());
3391  SMin.convertFromAPInt(APInt::getSignedMinValue(IntWidth), true,
3393  if (SMin.compare(RHS) == APFloat::cmpGreaterThan) { // smin > 12312.0
3394  if (Pred == ICmpInst::ICMP_NE || Pred == ICmpInst::ICMP_SGT ||
3395  Pred == ICmpInst::ICMP_SGE)
3396  return ReplaceInstUsesWith(I, Builder->getTrue());
3397  return ReplaceInstUsesWith(I, Builder->getFalse());
3398  }
3399  } else {
3400  // See if the RHS value is < UnsignedMin.
3401  APFloat SMin(RHS.getSemantics());
3402  SMin.convertFromAPInt(APInt::getMinValue(IntWidth), true,
3404  if (SMin.compare(RHS) == APFloat::cmpGreaterThan) { // umin > 12312.0
3405  if (Pred == ICmpInst::ICMP_NE || Pred == ICmpInst::ICMP_UGT ||
3406  Pred == ICmpInst::ICMP_UGE)
3407  return ReplaceInstUsesWith(I, Builder->getTrue());
3408  return ReplaceInstUsesWith(I, Builder->getFalse());
3409  }
3410  }
3411 
3412  // Okay, now we know that the FP constant fits in the range [SMIN, SMAX] or
3413  // [0, UMAX], but it may still be fractional. See if it is fractional by
3414  // casting the FP value to the integer value and back, checking for equality.
3415  // Don't do this for zero, because -0.0 is not fractional.
3416  Constant *RHSInt = LHSUnsigned
3417  ? ConstantExpr::getFPToUI(RHSC, IntTy)
3418  : ConstantExpr::getFPToSI(RHSC, IntTy);
3419  if (!RHS.isZero()) {
3420  bool Equal = LHSUnsigned
3421  ? ConstantExpr::getUIToFP(RHSInt, RHSC->getType()) == RHSC
3422  : ConstantExpr::getSIToFP(RHSInt, RHSC->getType()) == RHSC;
3423  if (!Equal) {
3424  // If we had a comparison against a fractional value, we have to adjust
3425  // the compare predicate and sometimes the value. RHSC is rounded towards
3426  // zero at this point.
3427  switch (Pred) {
3428  default: llvm_unreachable("Unexpected integer comparison!");
3429  case ICmpInst::ICMP_NE: // (float)int != 4.4 --> true
3430  return ReplaceInstUsesWith(I, Builder->getTrue());
3431  case ICmpInst::ICMP_EQ: // (float)int == 4.4 --> false
3432  return ReplaceInstUsesWith(I, Builder->getFalse());
3433  case ICmpInst::ICMP_ULE:
3434  // (float)int <= 4.4 --> int <= 4
3435  // (float)int <= -4.4 --> false
3436  if (RHS.isNegative())
3437  return ReplaceInstUsesWith(I, Builder->getFalse());
3438  break;
3439  case ICmpInst::ICMP_SLE:
3440  // (float)int <= 4.4 --> int <= 4
3441  // (float)int <= -4.4 --> int < -4
3442  if (RHS.isNegative())
3443  Pred = ICmpInst::ICMP_SLT;
3444  break;
3445  case ICmpInst::ICMP_ULT:
3446  // (float)int < -4.4 --> false
3447  // (float)int < 4.4 --> int <= 4
3448  if (RHS.isNegative())
3449  return ReplaceInstUsesWith(I, Builder->getFalse());
3450  Pred = ICmpInst::ICMP_ULE;
3451  break;
3452  case ICmpInst::ICMP_SLT:
3453  // (float)int < -4.4 --> int < -4
3454  // (float)int < 4.4 --> int <= 4
3455  if (!RHS.isNegative())
3456  Pred = ICmpInst::ICMP_SLE;
3457  break;
3458  case ICmpInst::ICMP_UGT:
3459  // (float)int > 4.4 --> int > 4
3460  // (float)int > -4.4 --> true
3461  if (RHS.isNegative())
3462  return ReplaceInstUsesWith(I, Builder->getTrue());
3463  break;
3464  case ICmpInst::ICMP_SGT:
3465  // (float)int > 4.4 --> int > 4
3466  // (float)int > -4.4 --> int >= -4
3467  if (RHS.isNegative())
3468  Pred = ICmpInst::ICMP_SGE;
3469  break;
3470  case ICmpInst::ICMP_UGE:
3471  // (float)int >= -4.4 --> true
3472  // (float)int >= 4.4 --> int > 4
3473  if (RHS.isNegative())
3474  return ReplaceInstUsesWith(I, Builder->getTrue());
3475  Pred = ICmpInst::ICMP_UGT;
3476  break;
3477  case ICmpInst::ICMP_SGE:
3478  // (float)int >= -4.4 --> int >= -4
3479  // (float)int >= 4.4 --> int > 4
3480  if (!RHS.isNegative())
3481  Pred = ICmpInst::ICMP_SGT;
3482  break;
3483  }
3484  }
3485  }
3486 
3487  // Lower this FP comparison into an appropriate integer version of the
3488  // comparison.
3489  return new ICmpInst(Pred, LHSI->getOperand(0), RHSInt);
3490 }
static Constant * getSIToFP(Constant *C, Type *Ty)
Definition: Constants.cpp:1664
bool isNaN() const
Returns true if and only if the float is a quiet or signaling NaN.
Definition: APFloat.h:387
static Constant * getFPToUI(Constant *C, Type *Ty)
Definition: Constants.cpp:1675
unsigned less or equal
Definition: InstrTypes.h:698
unsigned less than
Definition: InstrTypes.h:697
0 1 0 0 True if ordered and less than
Definition: InstrTypes.h:678
1 1 1 0 True if unordered or not equal
Definition: InstrTypes.h:688
int getFPMantissaWidth() const
Definition: Type.cpp:142
static Constant * getUIToFP(Constant *C, Type *Ty)
Definition: Constants.cpp:1653
static APInt getSignedMaxValue(unsigned numBits)
Gets maximum signed value of APInt for a specific bit width.
Definition: APInt.h:421
1 0 0 1 True if unordered or equal
Definition: InstrTypes.h:683
1 0 0 0 True if unordered: isnan(X) | isnan(Y)
Definition: InstrTypes.h:682
#define llvm_unreachable(msg)
Definition: ErrorHandling.h:98
0 1 0 1 True if ordered and less than or equal
Definition: InstrTypes.h:679
opStatus convertFromAPInt(const APInt &, bool, roundingMode)
Definition: APFloat.cpp:2258
A self-contained host- and target-independent arbitrary-precision floating-point software implementat...
Definition: APFloat.h:122
assert(Globals.size() > 1)
ConstantInt * getTrue()
Get the constant value for i1 true.
Definition: IRBuilder.h:256
LLVM Constant Representation.
Definition: Constant.h:41
Instruction * ReplaceInstUsesWith(Instruction &I, Value *V)
Definition: InstCombine.h:282
Represent an integer comparison operator.
Definition: Instructions.h:977
Value * getOperand(unsigned i) const
Definition: User.h:90
0 1 1 1 True if ordered (no nans)
Definition: InstrTypes.h:681
Integer representation type.
Definition: DerivedTypes.h:37
Predicate getPredicate() const
Return the predicate for this instruction.
Definition: InstrTypes.h:735
BuilderTy * Builder
Definition: InstCombine.h:102
1 1 0 1 True if unordered, less than, or equal
Definition: InstrTypes.h:687
signed greater than
Definition: InstrTypes.h:699
0 0 1 0 True if ordered and greater than
Definition: InstrTypes.h:676
bool isNegative() const
Definition: APFloat.h:362
unsigned getScalarSizeInBits() const LLVM_READONLY
Definition: Type.cpp:135
1 1 0 0 True if unordered or less than
Definition: InstrTypes.h:686
Type * getType() const
Definition: Value.h:215
signed less than
Definition: InstrTypes.h:701
static APInt getMinValue(unsigned numBits)
Gets minimum unsigned value of APInt for a specific bit width.
Definition: APInt.h:428
signed less or equal
Definition: InstrTypes.h:702
static APInt getMaxValue(unsigned numBits)
Gets maximum unsigned value of APInt for specific bit width.
Definition: APInt.h:416
ConstantInt * getFalse()
Get the constant value for i1 false.
Definition: IRBuilder.h:261
unsigned greater or equal
Definition: InstrTypes.h:696
0 1 1 0 True if ordered and operands are unequal
Definition: InstrTypes.h:680
1 0 1 0 True if unordered or greater than
Definition: InstrTypes.h:684
static APInt getSignedMinValue(unsigned numBits)
Gets minimum signed value of APInt for a specific bit width.
Definition: APInt.h:431
0 0 0 1 True if ordered and equal
Definition: InstrTypes.h:675
1 0 1 1 True if unordered, greater than, or equal
Definition: InstrTypes.h:685
bool isZero() const
Returns true if and only if the float is plus or minus zero.
Definition: APFloat.h:377
unsigned greater than
Definition: InstrTypes.h:695
0 0 1 1 True if ordered and greater than or equal
Definition: InstrTypes.h:677
const fltSemantics & getSemantics() const
Definition: APFloat.h:398
signed greater or equal
Definition: InstrTypes.h:700
Value * InstCombiner::foldFMulConst ( Instruction FMulOrDiv,
Constant C,
Instruction InsertBefore 
)

foldFMulConst() is a helper routine of InstCombiner::visitFMul(). The input FMulOrDiv is a FMul/FDiv with one and only one operand being a constant (i.e. isFMulOrFDivWithConstant(FMulOrDiv) == true). This function is to simplify "FMulOrDiv * C" and returns the resulting expression. Note that this function could return NULL in case the constants cannot be folded into a normal floating-point.

Definition at line 385 of file InstCombineMulDivRem.cpp.

386  {
387  assert(isFMulOrFDivWithConstant(FMulOrDiv) && "V is invalid");
388 
389  Value *Opnd0 = FMulOrDiv->getOperand(0);
390  Value *Opnd1 = FMulOrDiv->getOperand(1);
391 
392  Constant *C0 = dyn_cast<Constant>(Opnd0);
393  Constant *C1 = dyn_cast<Constant>(Opnd1);
394 
395  BinaryOperator *R = nullptr;
396 
397  // (X * C0) * C => X * (C0*C)
398  if (FMulOrDiv->getOpcode() == Instruction::FMul) {
399  Constant *F = ConstantExpr::getFMul(C1 ? C1 : C0, C);
400  if (isNormalFp(F))
401  R = BinaryOperator::CreateFMul(C1 ? Opnd0 : Opnd1, F);
402  } else {
403  if (C0) {
404  // (C0 / X) * C => (C0 * C) / X
405  if (FMulOrDiv->hasOneUse()) {
406  // It would otherwise introduce another div.
407  Constant *F = ConstantExpr::getFMul(C0, C);
408  if (isNormalFp(F))
409  R = BinaryOperator::CreateFDiv(F, Opnd1);
410  }
411  } else {
412  // (X / C1) * C => X * (C/C1) if C/C1 is not a denormal
413  Constant *F = ConstantExpr::getFDiv(C, C1);
414  if (isNormalFp(F)) {
415  R = BinaryOperator::CreateFMul(Opnd0, F);
416  } else {
417  // (X / C1) * C => X / (C1/C)
418  Constant *F = ConstantExpr::getFDiv(C1, C);
419  if (isNormalFp(F))
420  R = BinaryOperator::CreateFDiv(Opnd0, F);
421  }
422  }
423  }
424 
425  if (R) {
426  R->setHasUnsafeAlgebra(true);
427  InsertNewInstWith(R, *InsertBefore);
428  }
429 
430  return R;
431 }
Instruction * InsertNewInstWith(Instruction *New, Instruction &Old)
Definition: InstCombine.h:271
F(f)
static Constant * getFMul(Constant *C1, Constant *C2)
Definition: Constants.cpp:2131
static Constant * getFDiv(Constant *C1, Constant *C2)
Definition: Constants.cpp:2145
assert(Globals.size() > 1)
LLVM Constant Representation.
Definition: Constant.h:41
Value * getOperand(unsigned i) const
Definition: User.h:90
static bool isNormalFp(Constant *C)
static bool isFMulOrFDivWithConstant(Value *V)
LLVM_ATTRIBUTE_UNUSED_RESULT std::enable_if< !is_simple_type< Y >::value, typename cast_retty< X, const Y >::ret_type >::type dyn_cast(const Y &Val)
Definition: Casting.h:265
bool hasOneUse() const
Definition: Value.h:284
LLVM Value Representation.
Definition: Value.h:69
unsigned getOpcode() const
getOpcode() returns a member of one of the enums like Instruction::Add.
Definition: Instruction.h:86
Instruction * InstCombiner::FoldGEPICmp ( GEPOperator GEPLHS,
Value RHS,
ICmpInst::Predicate  Cond,
Instruction I 
)

FoldGEPICmp - Fold comparisons between a GEP instruction and something else. At this point we know that the GEP is on the LHS of the comparison.

Definition at line 604 of file InstCombineCompares.cpp.

606  {
607  // Don't transform signed compares of GEPs into index compares. Even if the
608  // GEP is inbounds, the final add of the base pointer can have signed overflow
609  // and would change the result of the icmp.
610  // e.g. "&foo[0] <s &foo[1]" can't be folded to "true" because "foo" could be
611  // the maximum signed value for the pointer type.
612  if (ICmpInst::isSigned(Cond))
613  return nullptr;
614 
615  // Look through bitcasts and addrspacecasts. We do not however want to remove
616  // 0 GEPs.
617  if (!isa<GetElementPtrInst>(RHS))
618  RHS = RHS->stripPointerCasts();
619 
620  Value *PtrBase = GEPLHS->getOperand(0);
621  if (DL && PtrBase == RHS && GEPLHS->isInBounds()) {
622  // ((gep Ptr, OFFSET) cmp Ptr) ---> (OFFSET cmp 0).
623  // This transformation (ignoring the base and scales) is valid because we
624  // know pointers can't overflow since the gep is inbounds. See if we can
625  // output an optimized form.
626  Value *Offset = EvaluateGEPOffsetExpression(GEPLHS, *this);
627 
628  // If not, synthesize the offset the hard way.
629  if (!Offset)
630  Offset = EmitGEPOffset(GEPLHS);
631  return new ICmpInst(ICmpInst::getSignedPredicate(Cond), Offset,
632  Constant::getNullValue(Offset->getType()));
633  } else if (GEPOperator *GEPRHS = dyn_cast<GEPOperator>(RHS)) {
634  // If the base pointers are different, but the indices are the same, just
635  // compare the base pointer.
636  if (PtrBase != GEPRHS->getOperand(0)) {
637  bool IndicesTheSame = GEPLHS->getNumOperands()==GEPRHS->getNumOperands();
638  IndicesTheSame &= GEPLHS->getOperand(0)->getType() ==
639  GEPRHS->getOperand(0)->getType();
640  if (IndicesTheSame)
641  for (unsigned i = 1, e = GEPLHS->getNumOperands(); i != e; ++i)
642  if (GEPLHS->getOperand(i) != GEPRHS->getOperand(i)) {
643  IndicesTheSame = false;
644  break;
645  }
646 
647  // If all indices are the same, just compare the base pointers.
648  if (IndicesTheSame)
649  return new ICmpInst(Cond, GEPLHS->getOperand(0), GEPRHS->getOperand(0));
650 
651  // If we're comparing GEPs with two base pointers that only differ in type
652  // and both GEPs have only constant indices or just one use, then fold
653  // the compare with the adjusted indices.
654  if (DL && GEPLHS->isInBounds() && GEPRHS->isInBounds() &&
655  (GEPLHS->hasAllConstantIndices() || GEPLHS->hasOneUse()) &&
656  (GEPRHS->hasAllConstantIndices() || GEPRHS->hasOneUse()) &&
657  PtrBase->stripPointerCasts() ==
658  GEPRHS->getOperand(0)->stripPointerCasts()) {
659  Value *LOffset = EmitGEPOffset(GEPLHS);
660  Value *ROffset = EmitGEPOffset(GEPRHS);
661 
662  // If we looked through an addrspacecast between different sized address
663  // spaces, the LHS and RHS pointers are different sized
664  // integers. Truncate to the smaller one.
665  Type *LHSIndexTy = LOffset->getType();
666  Type *RHSIndexTy = ROffset->getType();
667  if (LHSIndexTy != RHSIndexTy) {
668  if (LHSIndexTy->getPrimitiveSizeInBits() <
669  RHSIndexTy->getPrimitiveSizeInBits()) {
670  ROffset = Builder->CreateTrunc(ROffset, LHSIndexTy);
671  } else
672  LOffset = Builder->CreateTrunc(LOffset, RHSIndexTy);
673  }
674 
676  LOffset, ROffset);
677  return ReplaceInstUsesWith(I, Cmp);
678  }
679 
680  // Otherwise, the base pointers are different and the indices are
681  // different, bail out.
682  return nullptr;
683  }
684 
685  // If one of the GEPs has all zero indices, recurse.
686  if (GEPLHS->hasAllZeroIndices())
687  return FoldGEPICmp(GEPRHS, GEPLHS->getOperand(0),
689 
690  // If the other GEP has all zero indices, recurse.
691  if (GEPRHS->hasAllZeroIndices())
692  return FoldGEPICmp(GEPLHS, GEPRHS->getOperand(0), Cond, I);
693 
694  bool GEPsInBounds = GEPLHS->isInBounds() && GEPRHS->isInBounds();
695  if (GEPLHS->getNumOperands() == GEPRHS->getNumOperands()) {
696  // If the GEPs only differ by one index, compare it.
697  unsigned NumDifferences = 0; // Keep track of # differences.
698  unsigned DiffOperand = 0; // The operand that differs.
699  for (unsigned i = 1, e = GEPRHS->getNumOperands(); i != e; ++i)
700  if (GEPLHS->getOperand(i) != GEPRHS->getOperand(i)) {
701  if (GEPLHS->getOperand(i)->getType()->getPrimitiveSizeInBits() !=
702  GEPRHS->getOperand(i)->getType()->getPrimitiveSizeInBits()) {
703  // Irreconcilable differences.
704  NumDifferences = 2;
705  break;
706  } else {
707  if (NumDifferences++) break;
708  DiffOperand = i;
709  }
710  }
711 
712  if (NumDifferences == 0) // SAME GEP?
713  return ReplaceInstUsesWith(I, // No comparison is needed here.
715 
716  else if (NumDifferences == 1 && GEPsInBounds) {
717  Value *LHSV = GEPLHS->getOperand(DiffOperand);
718  Value *RHSV = GEPRHS->getOperand(DiffOperand);
719  // Make sure we do a signed comparison here.
720  return new ICmpInst(ICmpInst::getSignedPredicate(Cond), LHSV, RHSV);
721  }
722  }
723 
724  // Only lower this if the icmp is the only user of the GEP or if we expect
725  // the result to fold to a constant!
726  if (DL &&
727  GEPsInBounds &&
728  (isa<ConstantExpr>(GEPLHS) || GEPLHS->hasOneUse()) &&
729  (isa<ConstantExpr>(GEPRHS) || GEPRHS->hasOneUse())) {
730  // ((gep Ptr, OFFSET1) cmp (gep Ptr, OFFSET2) ---> (OFFSET1 cmp OFFSET2)
731  Value *L = EmitGEPOffset(GEPLHS);
732  Value *R = EmitGEPOffset(GEPRHS);
733  return new ICmpInst(ICmpInst::getSignedPredicate(Cond), L, R);
734  }
735  }
736  return nullptr;
737 }
Instruction * FoldGEPICmp(GEPOperator *GEPLHS, Value *RHS, ICmpInst::Predicate Cond, Instruction &I)
Value * CreateICmp(CmpInst::Predicate P, Value *LHS, Value *RHS, const Twine &Name="")
Definition: IRBuilder.h:1321
unsigned getNumOperands() const
Definition: User.h:110
bool isSigned() const
Determine if this instruction is using a signed comparison.
Definition: InstrTypes.h:801
static Constant * getNullValue(Type *Ty)
Definition: Constants.cpp:133
bool hasAllConstantIndices() const
Definition: Operator.h:432
ConstantInt * getInt1(bool V)
Get a constant value representing either true or false.
Definition: IRBuilder.h:251
Instruction * ReplaceInstUsesWith(Instruction &I, Value *V)
Definition: InstCombine.h:282
static Value * EvaluateGEPOffsetExpression(User *GEP, InstCombiner &IC)
Represent an integer comparison operator.
Definition: Instructions.h:977
Value * getOperand(unsigned i) const
Definition: User.h:90
BuilderTy * Builder
Definition: InstCombine.h:102
Value * EmitGEPOffset(User *GEP)
Type * getType() const
Definition: Value.h:215
bool hasAllZeroIndices() const
Definition: Operator.h:419
bool isTrueWhenEqual() const
Determine if this is true when both operands are the same.
Definition: InstrTypes.h:813
Value * stripPointerCasts()
Strips off any unneeded pointer casts, all-zero GEPs and aliases from the specified value...
Definition: Value.cpp:423
Predicate getSwappedPredicate() const
Return the predicate as if the operands were swapped.
Definition: InstrTypes.h:774
bool isInBounds() const
Definition: Operator.h:377
#define I(x, y, z)
Definition: MD5.cpp:54
bool hasOneUse() const
Definition: Value.h:284
const DataLayout * DL
Definition: InstCombine.h:89
unsigned getPrimitiveSizeInBits() const LLVM_READONLY
Definition: Type.cpp:117
Value * CreateTrunc(Value *V, Type *DestTy, const Twine &Name="")
Definition: IRBuilder.h:1094
LLVM Value Representation.
Definition: Value.h:69
Predicate getSignedPredicate() const
Return the signed version of the predicate.
Instruction * InstCombiner::FoldICmpAddOpCst ( Instruction ICI,
Value X,
ConstantInt CI,
ICmpInst::Predicate  Pred 
)

FoldICmpAddOpCst - Fold "icmp pred (X+CI), X".

Definition at line 740 of file InstCombineCompares.cpp.

742  {
743  // If we have X+0, exit early (simplifying logic below) and let it get folded
744  // elsewhere. icmp X+0, X -> icmp X, X
745  if (CI->isZero()) {
746  bool isTrue = ICmpInst::isTrueWhenEqual(Pred);
747  return ReplaceInstUsesWith(ICI, ConstantInt::get(ICI.getType(), isTrue));
748  }
749 
750  // (X+4) == X -> false.
751  if (Pred == ICmpInst::ICMP_EQ)
752  return ReplaceInstUsesWith(ICI, Builder->getFalse());
753 
754  // (X+4) != X -> true.
755  if (Pred == ICmpInst::ICMP_NE)
756  return ReplaceInstUsesWith(ICI, Builder->getTrue());
757 
758  // From this point on, we know that (X+C <= X) --> (X+C < X) because C != 0,
759  // so the values can never be equal. Similarly for all other "or equals"
760  // operators.
761 
762  // (X+1) <u X --> X >u (MAXUINT-1) --> X == 255
763  // (X+2) <u X --> X >u (MAXUINT-2) --> X > 253
764  // (X+MAXUINT) <u X --> X >u (MAXUINT-MAXUINT) --> X != 0
765  if (Pred == ICmpInst::ICMP_ULT || Pred == ICmpInst::ICMP_ULE) {
766  Value *R =
768  return new ICmpInst(ICmpInst::ICMP_UGT, X, R);
769  }
770 
771  // (X+1) >u X --> X <u (0-1) --> X != 255
772  // (X+2) >u X --> X <u (0-2) --> X <u 254
773  // (X+MAXUINT) >u X --> X <u (0-MAXUINT) --> X <u 1 --> X == 0
774  if (Pred == ICmpInst::ICMP_UGT || Pred == ICmpInst::ICMP_UGE)
776 
777  unsigned BitWidth = CI->getType()->getPrimitiveSizeInBits();
779  APInt::getSignedMaxValue(BitWidth));
780 
781  // (X+ 1) <s X --> X >s (MAXSINT-1) --> X == 127
782  // (X+ 2) <s X --> X >s (MAXSINT-2) --> X >s 125
783  // (X+MAXSINT) <s X --> X >s (MAXSINT-MAXSINT) --> X >s 0
784  // (X+MINSINT) <s X --> X >s (MAXSINT-MINSINT) --> X >s -1
785  // (X+ -2) <s X --> X >s (MAXSINT- -2) --> X >s 126
786  // (X+ -1) <s X --> X >s (MAXSINT- -1) --> X != 127
787  if (Pred == ICmpInst::ICMP_SLT || Pred == ICmpInst::ICMP_SLE)
788  return new ICmpInst(ICmpInst::ICMP_SGT, X, ConstantExpr::getSub(SMax, CI));
789 
790  // (X+ 1) >s X --> X <s (MAXSINT-(1-1)) --> X != 127
791  // (X+ 2) >s X --> X <s (MAXSINT-(2-1)) --> X <s 126
792  // (X+MAXSINT) >s X --> X <s (MAXSINT-(MAXSINT-1)) --> X <s 1
793  // (X+MINSINT) >s X --> X <s (MAXSINT-(MINSINT-1)) --> X <s -2
794  // (X+ -2) >s X --> X <s (MAXSINT-(-2-1)) --> X <s -126
795  // (X+ -1) >s X --> X <s (MAXSINT-(-1-1)) --> X == -128
796 
797  assert(Pred == ICmpInst::ICMP_SGT || Pred == ICmpInst::ICMP_SGE);
798  Constant *C = Builder->getInt(CI->getValue()-1);
799  return new ICmpInst(ICmpInst::ICMP_SLT, X, ConstantExpr::getSub(SMax, C));
800 }
IntegerType * getType() const
Definition: Constants.h:139
unsigned less or equal
Definition: InstrTypes.h:698
unsigned less than
Definition: InstrTypes.h:697
static Constant * getSub(Constant *C1, Constant *C2, bool HasNUW=false, bool HasNSW=false)
Definition: Constants.cpp:2113
static APInt getSignedMaxValue(unsigned numBits)
Gets maximum signed value of APInt for a specific bit width.
Definition: APInt.h:421
const APInt & getValue() const
Return the constant's value.
Definition: Constants.h:105
assert(Globals.size() > 1)
ConstantInt * getTrue()
Get the constant value for i1 true.
Definition: IRBuilder.h:256
LLVM Constant Representation.
Definition: Constant.h:41
Instruction * ReplaceInstUsesWith(Instruction &I, Value *V)
Definition: InstCombine.h:282
Represent an integer comparison operator.
Definition: Instructions.h:977
static Constant * getAllOnesValue(Type *Ty)
Get the all ones value.
Definition: Constants.cpp:185
BuilderTy * Builder
Definition: InstCombine.h:102
LLVMContext & getContext() const
All values hold a context through their type.
Definition: Value.cpp:615
signed greater than
Definition: InstrTypes.h:699
Class for constant integers.
Definition: Constants.h:51
Type * getType() const
Definition: Value.h:215
signed less than
Definition: InstrTypes.h:701
bool isTrueWhenEqual() const
Determine if this is true when both operands are the same.
Definition: InstrTypes.h:813
static Constant * get(Type *Ty, uint64_t V, bool isSigned=false)
Definition: Constants.cpp:528
bool isZero() const
Definition: Constants.h:160
signed less or equal
Definition: InstrTypes.h:702
ConstantInt * getFalse()
Get the constant value for i1 false.
Definition: IRBuilder.h:261
static Constant * getNeg(Constant *C, bool HasNUW=false, bool HasNSW=false)
Definition: Constants.cpp:2083
unsigned greater or equal
Definition: InstrTypes.h:696
unsigned getPrimitiveSizeInBits() const LLVM_READONLY
Definition: Type.cpp:117
LLVM Value Representation.
Definition: Value.h:69
unsigned greater than
Definition: InstrTypes.h:695
ConstantInt * getInt(const APInt &AI)
Get a constant integer value.
Definition: IRBuilder.h:292
signed greater or equal
Definition: InstrTypes.h:700
Instruction * InstCombiner::FoldICmpDivCst ( ICmpInst ICI,
BinaryOperator DivI,
ConstantInt DivRHS 
)

FoldICmpDivCst - Fold "icmp pred, ([su]div X, DivRHS), CmpRHS" where DivRHS and CmpRHS are both known to be integer constants.

If the division is known to be exact, then there is no remainder from the divide, so the covered range size is unit, otherwise it is the divisor.

Definition at line 804 of file InstCombineCompares.cpp.

805  {
806  ConstantInt *CmpRHS = cast<ConstantInt>(ICI.getOperand(1));
807  const APInt &CmpRHSV = CmpRHS->getValue();
808 
809  // FIXME: If the operand types don't match the type of the divide
810  // then don't attempt this transform. The code below doesn't have the
811  // logic to deal with a signed divide and an unsigned compare (and
812  // vice versa). This is because (x /s C1) <s C2 produces different
813  // results than (x /s C1) <u C2 or (x /u C1) <s C2 or even
814  // (x /u C1) <u C2. Simply casting the operands and result won't
815  // work. :( The if statement below tests that condition and bails
816  // if it finds it.
817  bool DivIsSigned = DivI->getOpcode() == Instruction::SDiv;
818  if (!ICI.isEquality() && DivIsSigned != ICI.isSigned())
819  return nullptr;
820  if (DivRHS->isZero())
821  return nullptr; // The ProdOV computation fails on divide by zero.
822  if (DivIsSigned && DivRHS->isAllOnesValue())
823  return nullptr; // The overflow computation also screws up here
824  if (DivRHS->isOne()) {
825  // This eliminates some funny cases with INT_MIN.
826  ICI.setOperand(0, DivI->getOperand(0)); // X/1 == X.
827  return &ICI;
828  }
829 
830  // Compute Prod = CI * DivRHS. We are essentially solving an equation
831  // of form X/C1=C2. We solve for X by multiplying C1 (DivRHS) and
832  // C2 (CI). By solving for X we can turn this into a range check
833  // instead of computing a divide.
834  Constant *Prod = ConstantExpr::getMul(CmpRHS, DivRHS);
835 
836  // Determine if the product overflows by seeing if the product is
837  // not equal to the divide. Make sure we do the same kind of divide
838  // as in the LHS instruction that we're folding.
839  bool ProdOV = (DivIsSigned ? ConstantExpr::getSDiv(Prod, DivRHS) :
840  ConstantExpr::getUDiv(Prod, DivRHS)) != CmpRHS;
841 
842  // Get the ICmp opcode
843  ICmpInst::Predicate Pred = ICI.getPredicate();
844 
845  /// If the division is known to be exact, then there is no remainder from the
846  /// divide, so the covered range size is unit, otherwise it is the divisor.
847  ConstantInt *RangeSize = DivI->isExact() ? getOne(Prod) : DivRHS;
848 
849  // Figure out the interval that is being checked. For example, a comparison
850  // like "X /u 5 == 0" is really checking that X is in the interval [0, 5).
851  // Compute this interval based on the constants involved and the signedness of
852  // the compare/divide. This computes a half-open interval, keeping track of
853  // whether either value in the interval overflows. After analysis each
854  // overflow variable is set to 0 if it's corresponding bound variable is valid
855  // -1 if overflowed off the bottom end, or +1 if overflowed off the top end.
856  int LoOverflow = 0, HiOverflow = 0;
857  Constant *LoBound = nullptr, *HiBound = nullptr;
858 
859  if (!DivIsSigned) { // udiv
860  // e.g. X/5 op 3 --> [15, 20)
861  LoBound = Prod;
862  HiOverflow = LoOverflow = ProdOV;
863  if (!HiOverflow) {
864  // If this is not an exact divide, then many values in the range collapse
865  // to the same result value.
866  HiOverflow = AddWithOverflow(HiBound, LoBound, RangeSize, false);
867  }
868 
869  } else if (DivRHS->getValue().isStrictlyPositive()) { // Divisor is > 0.
870  if (CmpRHSV == 0) { // (X / pos) op 0
871  // Can't overflow. e.g. X/2 op 0 --> [-1, 2)
872  LoBound = ConstantExpr::getNeg(SubOne(RangeSize));
873  HiBound = RangeSize;
874  } else if (CmpRHSV.isStrictlyPositive()) { // (X / pos) op pos
875  LoBound = Prod; // e.g. X/5 op 3 --> [15, 20)
876  HiOverflow = LoOverflow = ProdOV;
877  if (!HiOverflow)
878  HiOverflow = AddWithOverflow(HiBound, Prod, RangeSize, true);
879  } else { // (X / pos) op neg
880  // e.g. X/5 op -3 --> [-15-4, -15+1) --> [-19, -14)
881  HiBound = AddOne(Prod);
882  LoOverflow = HiOverflow = ProdOV ? -1 : 0;
883  if (!LoOverflow) {
884  ConstantInt *DivNeg =cast<ConstantInt>(ConstantExpr::getNeg(RangeSize));
885  LoOverflow = AddWithOverflow(LoBound, HiBound, DivNeg, true) ? -1 : 0;
886  }
887  }
888  } else if (DivRHS->isNegative()) { // Divisor is < 0.
889  if (DivI->isExact())
890  RangeSize = cast<ConstantInt>(ConstantExpr::getNeg(RangeSize));
891  if (CmpRHSV == 0) { // (X / neg) op 0
892  // e.g. X/-5 op 0 --> [-4, 5)
893  LoBound = AddOne(RangeSize);
894  HiBound = cast<ConstantInt>(ConstantExpr::getNeg(RangeSize));
895  if (HiBound == DivRHS) { // -INTMIN = INTMIN
896  HiOverflow = 1; // [INTMIN+1, overflow)
897  HiBound = nullptr; // e.g. X/INTMIN = 0 --> X > INTMIN
898  }
899  } else if (CmpRHSV.isStrictlyPositive()) { // (X / neg) op pos
900  // e.g. X/-5 op 3 --> [-19, -14)
901  HiBound = AddOne(Prod);
902  HiOverflow = LoOverflow = ProdOV ? -1 : 0;
903  if (!LoOverflow)
904  LoOverflow = AddWithOverflow(LoBound, HiBound, RangeSize, true) ? -1:0;
905  } else { // (X / neg) op neg
906  LoBound = Prod; // e.g. X/-5 op -3 --> [15, 20)
907  LoOverflow = HiOverflow = ProdOV;
908  if (!HiOverflow)
909  HiOverflow = SubWithOverflow(HiBound, Prod, RangeSize, true);
910  }
911 
912  // Dividing by a negative swaps the condition. LT <-> GT
913  Pred = ICmpInst::getSwappedPredicate(Pred);
914  }
915 
916  Value *X = DivI->getOperand(0);
917  switch (Pred) {
918  default: llvm_unreachable("Unhandled icmp opcode!");
919  case ICmpInst::ICMP_EQ:
920  if (LoOverflow && HiOverflow)
921  return ReplaceInstUsesWith(ICI, Builder->getFalse());
922  if (HiOverflow)
923  return new ICmpInst(DivIsSigned ? ICmpInst::ICMP_SGE :
924  ICmpInst::ICMP_UGE, X, LoBound);
925  if (LoOverflow)
926  return new ICmpInst(DivIsSigned ? ICmpInst::ICMP_SLT :
927  ICmpInst::ICMP_ULT, X, HiBound);
928  return ReplaceInstUsesWith(ICI, InsertRangeTest(X, LoBound, HiBound,
929  DivIsSigned, true));
930  case ICmpInst::ICMP_NE:
931  if (LoOverflow && HiOverflow)
932  return ReplaceInstUsesWith(ICI, Builder->getTrue());
933  if (HiOverflow)
934  return new ICmpInst(DivIsSigned ? ICmpInst::ICMP_SLT :
935  ICmpInst::ICMP_ULT, X, LoBound);
936  if (LoOverflow)
937  return new ICmpInst(DivIsSigned ? ICmpInst::ICMP_SGE :
938  ICmpInst::ICMP_UGE, X, HiBound);
939  return ReplaceInstUsesWith(ICI, InsertRangeTest(X, LoBound, HiBound,
940  DivIsSigned, false));
941  case ICmpInst::ICMP_ULT:
942  case ICmpInst::ICMP_SLT:
943  if (LoOverflow == +1) // Low bound is greater than input range.
944  return ReplaceInstUsesWith(ICI, Builder->getTrue());
945  if (LoOverflow == -1) // Low bound is less than input range.
946  return ReplaceInstUsesWith(ICI, Builder->getFalse());
947  return new ICmpInst(Pred, X, LoBound);
948  case ICmpInst::ICMP_UGT:
949  case ICmpInst::ICMP_SGT:
950  if (HiOverflow == +1) // High bound greater than input range.
951  return ReplaceInstUsesWith(ICI, Builder->getFalse());
952  if (HiOverflow == -1) // High bound less than input range.
953  return ReplaceInstUsesWith(ICI, Builder->getTrue());
954  if (Pred == ICmpInst::ICMP_UGT)
955  return new ICmpInst(ICmpInst::ICMP_UGE, X, HiBound);
956  return new ICmpInst(ICmpInst::ICMP_SGE, X, HiBound);
957  }
958 }
static bool SubWithOverflow(Constant *&Result, Constant *In1, Constant *In2, bool IsSigned=false)
unsigned less than
Definition: InstrTypes.h:697
bool isSigned() const
Determine if this instruction is using a signed comparison.
Definition: InstrTypes.h:801
static RegisterPass< NVPTXAllocaHoisting > X("alloca-hoisting","Hoisting alloca instructions in non-entry ""blocks to the entry block")
static bool isEquality(Predicate P)
const APInt & getValue() const
Return the constant's value.
Definition: Constants.h:105
#define llvm_unreachable(msg)
Definition: ErrorHandling.h:98
static Constant * AddOne(Constant *C)
AddOne - Add one to a Constant.
Definition: InstCombine.h:60
bool isNegative() const
Definition: Constants.h:155
static bool AddWithOverflow(Constant *&Result, Constant *In1, Constant *In2, bool IsSigned=false)
ConstantInt * getTrue()
Get the constant value for i1 true.
Definition: IRBuilder.h:256
LLVM Constant Representation.
Definition: Constant.h:41
Instruction * ReplaceInstUsesWith(Instruction &I, Value *V)
Definition: InstCombine.h:282
Represent an integer comparison operator.
Definition: Instructions.h:977
Value * InsertRangeTest(Value *V, Constant *Lo, Constant *Hi, bool isSigned, bool Inside)
Value * getOperand(unsigned i) const
Definition: User.h:90
Predicate getPredicate() const
Return the predicate for this instruction.
Definition: InstrTypes.h:735
BuilderTy * Builder
Definition: InstCombine.h:102
signed greater than
Definition: InstrTypes.h:699
BinaryOps getOpcode() const
Definition: InstrTypes.h:326
Class for constant integers.
Definition: Constants.h:51
static Constant * getSDiv(Constant *C1, Constant *C2, bool isExact=false)
Definition: Constants.cpp:2140
signed less than
Definition: InstrTypes.h:701
bool isStrictlyPositive() const
Determine if this APInt Value is positive.
Definition: APInt.h:333
Predicate getSwappedPredicate() const
Return the predicate as if the operands were swapped.
Definition: InstrTypes.h:774
bool isZero() const
Definition: Constants.h:160
bool isExact() const
isExact - Determine whether the exact flag is set.
void setOperand(unsigned i, Value *Val)
Definition: User.h:94
bool isAllOnesValue() const
Definition: Constants.cpp:88
Class for arbitrary precision integers.
Definition: APInt.h:75
static ConstantInt * getOne(Constant *C)
ConstantInt * getFalse()
Get the constant value for i1 false.
Definition: IRBuilder.h:261
static Constant * getNeg(Constant *C, bool HasNUW=false, bool HasNSW=false)
Definition: Constants.cpp:2083
unsigned greater or equal
Definition: InstrTypes.h:696
LLVM Value Representation.
Definition: Value.h:69
unsigned greater than
Definition: InstrTypes.h:695
static Constant * getMul(Constant *C1, Constant *C2, bool HasNUW=false, bool HasNSW=false)
Definition: Constants.cpp:2124
static Constant * SubOne(Constant *C)
SubOne - Subtract one from a Constant.
Definition: InstCombine.h:64
bool isOne() const
Determine if the value is one.
Definition: Constants.h:168
signed greater or equal
Definition: InstrTypes.h:700
Instruction * InstCombiner::FoldICmpShrCst ( ICmpInst ICI,
BinaryOperator DivI,
ConstantInt DivRHS 
)

FoldICmpShrCst - Handle "icmp(([al]shr X, cst1), cst2)".

Definition at line 961 of file InstCombineCompares.cpp.

962  {
963  const APInt &CmpRHSV = cast<ConstantInt>(ICI.getOperand(1))->getValue();
964 
965  // Check that the shift amount is in range. If not, don't perform
966  // undefined shifts. When the shift is visited it will be
967  // simplified.
968  uint32_t TypeBits = CmpRHSV.getBitWidth();
969  uint32_t ShAmtVal = (uint32_t)ShAmt->getLimitedValue(TypeBits);
970  if (ShAmtVal >= TypeBits || ShAmtVal == 0)
971  return nullptr;
972 
973  if (!ICI.isEquality()) {
974  // If we have an unsigned comparison and an ashr, we can't simplify this.
975  // Similarly for signed comparisons with lshr.
976  if (ICI.isSigned() != (Shr->getOpcode() == Instruction::AShr))
977  return nullptr;
978 
979  // Otherwise, all lshr and most exact ashr's are equivalent to a udiv/sdiv
980  // by a power of 2. Since we already have logic to simplify these,
981  // transform to div and then simplify the resultant comparison.
982  if (Shr->getOpcode() == Instruction::AShr &&
983  (!Shr->isExact() || ShAmtVal == TypeBits - 1))
984  return nullptr;
985 
986  // Revisit the shift (to delete it).
987  Worklist.Add(Shr);
988 
989  Constant *DivCst =
990  ConstantInt::get(Shr->getType(), APInt::getOneBitSet(TypeBits, ShAmtVal));
991 
992  Value *Tmp =
993  Shr->getOpcode() == Instruction::AShr ?
994  Builder->CreateSDiv(Shr->getOperand(0), DivCst, "", Shr->isExact()) :
995  Builder->CreateUDiv(Shr->getOperand(0), DivCst, "", Shr->isExact());
996 
997  ICI.setOperand(0, Tmp);
998 
999  // If the builder folded the binop, just return it.
1000  BinaryOperator *TheDiv = dyn_cast<BinaryOperator>(Tmp);
1001  if (!TheDiv)
1002  return &ICI;
1003 
1004  // Otherwise, fold this div/compare.
1005  assert(TheDiv->getOpcode() == Instruction::SDiv ||
1006  TheDiv->getOpcode() == Instruction::UDiv);
1007 
1008  Instruction *Res = FoldICmpDivCst(ICI, TheDiv, cast<ConstantInt>(DivCst));
1009  assert(Res && "This div/cst should have folded!");
1010  return Res;
1011  }
1012 
1013 
1014  // If we are comparing against bits always shifted out, the
1015  // comparison cannot succeed.
1016  APInt Comp = CmpRHSV << ShAmtVal;
1017  ConstantInt *ShiftedCmpRHS = Builder->getInt(Comp);
1018  if (Shr->getOpcode() == Instruction::LShr)
1019  Comp = Comp.lshr(ShAmtVal);
1020  else
1021  Comp = Comp.ashr(ShAmtVal);
1022 
1023  if (Comp != CmpRHSV) { // Comparing against a bit that we know is zero.
1024  bool IsICMP_NE = ICI.getPredicate() == ICmpInst::ICMP_NE;
1025  Constant *Cst = Builder->getInt1(IsICMP_NE);
1026  return ReplaceInstUsesWith(ICI, Cst);
1027  }
1028 
1029  // Otherwise, check to see if the bits shifted out are known to be zero.
1030  // If so, we can compare against the unshifted value:
1031  // (X & 4) >> 1 == 2 --> (X & 4) == 4.
1032  if (Shr->hasOneUse() && Shr->isExact())
1033  return new ICmpInst(ICI.getPredicate(), Shr->getOperand(0), ShiftedCmpRHS);
1034 
1035  if (Shr->hasOneUse()) {
1036  // Otherwise strength reduce the shift into an and.
1037  APInt Val(APInt::getHighBitsSet(TypeBits, TypeBits - ShAmtVal));
1038  Constant *Mask = Builder->getInt(Val);
1039 
1040  Value *And = Builder->CreateAnd(Shr->getOperand(0),
1041  Mask, Shr->getName()+".mask");
1042  return new ICmpInst(ICI.getPredicate(), And, ShiftedCmpRHS);
1043  }
1044  return nullptr;
1045 }
APInt LLVM_ATTRIBUTE_UNUSED_RESULT ashr(unsigned shiftAmt) const
Arithmetic right-shift function.
Definition: APInt.cpp:1039
bool isSigned() const
Determine if this instruction is using a signed comparison.
Definition: InstrTypes.h:801
void Add(Instruction *I)
static bool isEquality(Predicate P)
StringRef getName() const
Definition: Value.cpp:168
APInt LLVM_ATTRIBUTE_UNUSED_RESULT lshr(unsigned shiftAmt) const
Logical right-shift function.
Definition: APInt.cpp:1128
Value * CreateAnd(Value *LHS, Value *RHS, const Twine &Name="")
Definition: IRBuilder.h:806
static APInt getHighBitsSet(unsigned numBits, unsigned hiBitsSet)
Get a value with high bits set.
Definition: APInt.h:508
assert(Globals.size() > 1)
ConstantInt * getInt1(bool V)
Get a constant value representing either true or false.
Definition: IRBuilder.h:251
LLVM Constant Representation.
Definition: Constant.h:41
Instruction * ReplaceInstUsesWith(Instruction &I, Value *V)
Definition: InstCombine.h:282
static APInt getOneBitSet(unsigned numBits, unsigned BitNo)
Return an APInt with exactly one bit set in the result.
Definition: APInt.h:474
Represent an integer comparison operator.
Definition: Instructions.h:977
unsigned getBitWidth() const
Return the number of bits in the APInt.
Definition: APInt.h:1248
Value * getOperand(unsigned i) const
Definition: User.h:90
Predicate getPredicate() const
Return the predicate for this instruction.
Definition: InstrTypes.h:735
InstCombineWorklist Worklist
Worklist - All of the instructions that need to be simplified.
Definition: InstCombine.h:97
BuilderTy * Builder
Definition: InstCombine.h:102
BinaryOps getOpcode() const
Definition: InstrTypes.h:326
Class for constant integers.
Definition: Constants.h:51
static Constant * get(Type *Ty, uint64_t V, bool isSigned=false)
Definition: Constants.cpp:528
void setOperand(unsigned i, Value *Val)
Definition: User.h:94
Class for arbitrary precision integers.
Definition: APInt.h:75
Value * CreateSDiv(Value *LHS, Value *RHS, const Twine &Name="", bool isExact=false)
Definition: IRBuilder.h:710
Value * CreateUDiv(Value *LHS, Value *RHS, const Twine &Name="", bool isExact=false)
Definition: IRBuilder.h:698
LLVM_ATTRIBUTE_UNUSED_RESULT std::enable_if< !is_simple_type< Y >::value, typename cast_retty< X, const Y >::ret_type >::type dyn_cast(const Y &Val)
Definition: Casting.h:265
APInt And(const APInt &LHS, const APInt &RHS)
Bitwise AND function for APInt.
Definition: APInt.h:1865
Instruction * FoldICmpDivCst(ICmpInst &ICI, BinaryOperator *DivI, ConstantInt *DivRHS)
LLVM Value Representation.
Definition: Value.h:69
ConstantInt * getInt(const APInt &AI)
Get a constant integer value.
Definition: IRBuilder.h:292
Value * InstCombiner::FoldLogicalPlusAnd ( Value LHS,
Value RHS,
ConstantInt Mask,
bool  isSub,
Instruction I 
)
private

FoldLogicalPlusAnd - This is part of an expression (LHS +/- RHS) & Mask, where isSub determines whether the operator is a sub. If we can fold one of the following xforms:

((A & N) +/- B) & Mask -> (A +/- B) & Mask iff N&Mask == Mask ((A | N) +/- B) & Mask -> (A +/- B) & Mask iff N&Mask == 0 ((A ^ N) +/- B) & Mask -> (A +/- B) & Mask iff N&Mask == 0

return (A +/- B).

Definition at line 332 of file InstCombineAndOrXor.cpp.

334  {
335  Instruction *LHSI = dyn_cast<Instruction>(LHS);
336  if (!LHSI || LHSI->getNumOperands() != 2 ||
337  !isa<ConstantInt>(LHSI->getOperand(1))) return nullptr;
338 
339  ConstantInt *N = cast<ConstantInt>(LHSI->getOperand(1));
340 
341  switch (LHSI->getOpcode()) {
342  default: return nullptr;
343  case Instruction::And:
344  if (ConstantExpr::getAnd(N, Mask) == Mask) {
345  // If the AndRHS is a power of two minus one (0+1+), this is simple.
346  if ((Mask->getValue().countLeadingZeros() +
347  Mask->getValue().countPopulation()) ==
348  Mask->getValue().getBitWidth())
349  break;
350 
351  // Otherwise, if Mask is 0+1+0+, and if B is known to have the low 0+
352  // part, we don't need any explicit masks to take them out of A. If that
353  // is all N is, ignore it.
354  uint32_t MB = 0, ME = 0;
355  if (isRunOfOnes(Mask, MB, ME)) { // begin/end bit of run, inclusive
356  uint32_t BitWidth = cast<IntegerType>(RHS->getType())->getBitWidth();
357  APInt Mask(APInt::getLowBitsSet(BitWidth, MB-1));
358  if (MaskedValueIsZero(RHS, Mask))
359  break;
360  }
361  }
362  return nullptr;
363  case Instruction::Or:
364  case Instruction::Xor:
365  // If the AndRHS is a power of two minus one (0+1+), and N&Mask == 0
366  if ((Mask->getValue().countLeadingZeros() +
367  Mask->getValue().countPopulation()) == Mask->getValue().getBitWidth()
368  && ConstantExpr::getAnd(N, Mask)->isNullValue())
369  break;
370  return nullptr;
371  }
372 
373  if (isSub)
374  return Builder->CreateSub(LHSI->getOperand(0), RHS, "fold");
375  return Builder->CreateAdd(LHSI->getOperand(0), RHS, "fold");
376 }
unsigned getNumOperands() const
Definition: User.h:110
static APInt getLowBitsSet(unsigned numBits, unsigned loBitsSet)
Get a value with low bits set.
Definition: APInt.h:526
Value * CreateSub(Value *LHS, Value *RHS, const Twine &Name="", bool HasNUW=false, bool HasNSW=false)
Definition: IRBuilder.h:654
static unsigned getBitWidth(Type *Ty, const DataLayout *TD)
const APInt & getValue() const
Return the constant's value.
Definition: Constants.h:105
static bool isRunOfOnes(ConstantInt *Val, uint32_t &MB, uint32_t &ME)
Value * CreateAdd(Value *LHS, Value *RHS, const Twine &Name="", bool HasNUW=false, bool HasNSW=false)
Definition: IRBuilder.h:632
static Constant * getAnd(Constant *C1, Constant *C2)
Definition: Constants.cpp:2161
APInt Or(const APInt &LHS, const APInt &RHS)
Bitwise OR function for APInt.
Definition: APInt.h:1870
APInt Xor(const APInt &LHS, const APInt &RHS)
Bitwise XOR function for APInt.
Definition: APInt.h:1875
unsigned getBitWidth() const
Return the number of bits in the APInt.
Definition: APInt.h:1248
Value * getOperand(unsigned i) const
Definition: User.h:90
unsigned countPopulation() const
Count the number of bits set.
Definition: APInt.h:1390
BuilderTy * Builder
Definition: InstCombine.h:102
Class for constant integers.
Definition: Constants.h:51
Type * getType() const
Definition: Value.h:215
bool isNullValue() const
Definition: Constants.cpp:75
Class for arbitrary precision integers.
Definition: APInt.h:75
LLVM_ATTRIBUTE_UNUSED_RESULT std::enable_if< !is_simple_type< Y >::value, typename cast_retty< X, const Y >::ret_type >::type dyn_cast(const Y &Val)
Definition: Casting.h:265
APInt And(const APInt &LHS, const APInt &RHS)
Bitwise AND function for APInt.
Definition: APInt.h:1865
#define N
bool isSub(Instruction *instr)
bool MaskedValueIsZero(Value *V, const APInt &Mask, unsigned Depth=0) const
Definition: InstCombine.h:323
unsigned getOpcode() const
getOpcode() returns a member of one of the enums like Instruction::Add.
Definition: Instruction.h:86
unsigned countLeadingZeros() const
The APInt version of the countLeadingZeros functions in MathExtras.h.
Definition: APInt.h:1336
Instruction * InstCombiner::FoldOpIntoPhi ( Instruction I)
private

FoldOpIntoPhi - Given a binary operator, cast instruction, or select which has a PHI node as operand #0, see if we can fold the instruction into the PHI (which is only possible if all operands to the PHI are constants).

Definition at line 724 of file InstructionCombining.cpp.

724  {
725  PHINode *PN = cast<PHINode>(I.getOperand(0));
726  unsigned NumPHIValues = PN->getNumIncomingValues();
727  if (NumPHIValues == 0)
728  return nullptr;
729 
730  // We normally only transform phis with a single use. However, if a PHI has
731  // multiple uses and they are all the same operation, we can fold *all* of the
732  // uses into the PHI.
733  if (!PN->hasOneUse()) {
734  // Walk the use list for the instruction, comparing them to I.
735  for (User *U : PN->users()) {
736  Instruction *UI = cast<Instruction>(U);
737  if (UI != &I && !I.isIdenticalTo(UI))
738  return nullptr;
739  }
740  // Otherwise, we can replace *all* users with the new PHI we form.
741  }
742 
743  // Check to see if all of the operands of the PHI are simple constants
744  // (constantint/constantfp/undef). If there is one non-constant value,
745  // remember the BB it is in. If there is more than one or if *it* is a PHI,
746  // bail out. We don't do arbitrary constant expressions here because moving
747  // their computation can be expensive without a cost model.
748  BasicBlock *NonConstBB = nullptr;
749  for (unsigned i = 0; i != NumPHIValues; ++i) {
750  Value *InVal = PN->getIncomingValue(i);
751  if (isa<Constant>(InVal) && !isa<ConstantExpr>(InVal))
752  continue;
753 
754  if (isa<PHINode>(InVal)) return nullptr; // Itself a phi.
755  if (NonConstBB) return nullptr; // More than one non-const value.
756 
757  NonConstBB = PN->getIncomingBlock(i);
758 
759  // If the InVal is an invoke at the end of the pred block, then we can't
760  // insert a computation after it without breaking the edge.
761  if (InvokeInst *II = dyn_cast<InvokeInst>(InVal))
762  if (II->getParent() == NonConstBB)
763  return nullptr;
764 
765  // If the incoming non-constant value is in I's block, we will remove one
766  // instruction, but insert another equivalent one, leading to infinite
767  // instcombine.
768  if (NonConstBB == I.getParent())
769  return nullptr;
770  }
771 
772  // If there is exactly one non-constant value, we can insert a copy of the
773  // operation in that block. However, if this is a critical edge, we would be
774  // inserting the computation one some other paths (e.g. inside a loop). Only
775  // do this if the pred block is unconditionally branching into the phi block.
776  if (NonConstBB != nullptr) {
777  BranchInst *BI = dyn_cast<BranchInst>(NonConstBB->getTerminator());
778  if (!BI || !BI->isUnconditional()) return nullptr;
779  }
780 
781  // Okay, we can do the transformation: create the new PHI node.
783  InsertNewInstBefore(NewPN, *PN);
784  NewPN->takeName(PN);
785 
786  // If we are going to have to insert a new computation, do so right before the
787  // predecessors terminator.
788  if (NonConstBB)
789  Builder->SetInsertPoint(NonConstBB->getTerminator());
790 
791  // Next, add all of the operands to the PHI.
792  if (SelectInst *SI = dyn_cast<SelectInst>(&I)) {
793  // We only currently try to fold the condition of a select when it is a phi,
794  // not the true/false values.
795  Value *TrueV = SI->getTrueValue();
796  Value *FalseV = SI->getFalseValue();
797  BasicBlock *PhiTransBB = PN->getParent();
798  for (unsigned i = 0; i != NumPHIValues; ++i) {
799  BasicBlock *ThisBB = PN->getIncomingBlock(i);
800  Value *TrueVInPred = TrueV->DoPHITranslation(PhiTransBB, ThisBB);
801  Value *FalseVInPred = FalseV->DoPHITranslation(PhiTransBB, ThisBB);
802  Value *InV = nullptr;
803  // Beware of ConstantExpr: it may eventually evaluate to getNullValue,
804  // even if currently isNullValue gives false.
805  Constant *InC = dyn_cast<Constant>(PN->getIncomingValue(i));
806  if (InC && !isa<ConstantExpr>(InC))
807  InV = InC->isNullValue() ? FalseVInPred : TrueVInPred;
808  else
809  InV = Builder->CreateSelect(PN->getIncomingValue(i),
810  TrueVInPred, FalseVInPred, "phitmp");
811  NewPN->addIncoming(InV, ThisBB);
812  }
813  } else if (CmpInst *CI = dyn_cast<CmpInst>(&I)) {
814  Constant *C = cast<Constant>(I.getOperand(1));
815  for (unsigned i = 0; i != NumPHIValues; ++i) {
816  Value *InV = nullptr;
817  if (Constant *InC = dyn_cast<Constant>(PN->getIncomingValue(i)))
818  InV = ConstantExpr::getCompare(CI->getPredicate(), InC, C);
819  else if (isa<ICmpInst>(CI))
820  InV = Builder->CreateICmp(CI->getPredicate(), PN->getIncomingValue(i),
821  C, "phitmp");
822  else
823  InV = Builder->CreateFCmp(CI->getPredicate(), PN->getIncomingValue(i),
824  C, "phitmp");
825  NewPN->addIncoming(InV, PN->getIncomingBlock(i));
826  }
827  } else if (I.getNumOperands() == 2) {
828  Constant *C = cast<Constant>(I.getOperand(1));
829  for (unsigned i = 0; i != NumPHIValues; ++i) {
830  Value *InV = nullptr;
831  if (Constant *InC = dyn_cast<Constant>(PN->getIncomingValue(i)))
832  InV = ConstantExpr::get(I.getOpcode(), InC, C);
833  else
834  InV = Builder->CreateBinOp(cast<BinaryOperator>(I).getOpcode(),
835  PN->getIncomingValue(i), C, "phitmp");
836  NewPN->addIncoming(InV, PN->getIncomingBlock(i));
837  }
838  } else {
839  CastInst *CI = cast<CastInst>(&I);
840  Type *RetTy = CI->getType();
841  for (unsigned i = 0; i != NumPHIValues; ++i) {
842  Value *InV;
843  if (Constant *InC = dyn_cast<Constant>(PN->getIncomingValue(i)))
844  InV = ConstantExpr::getCast(CI->getOpcode(), InC, RetTy);
845  else
846  InV = Builder->CreateCast(CI->getOpcode(),
847  PN->getIncomingValue(i), I.getType(), "phitmp");
848  NewPN->addIncoming(InV, PN->getIncomingBlock(i));
849  }
850  }
851 
852  for (auto UI = PN->user_begin(), E = PN->user_end(); UI != E;) {
853  Instruction *User = cast<Instruction>(*UI++);
854  if (User == &I) continue;
855  ReplaceInstUsesWith(*User, NewPN);
856  EraseInstFromFunction(*User);
857  }
858  return ReplaceInstUsesWith(I, NewPN);
859 }
Instruction::CastOps getOpcode() const
Return the opcode of this CastInst.
Definition: InstrTypes.h:624
Abstract base class of comparison instructions.
Definition: InstrTypes.h:654
Value * CreateCast(Instruction::CastOps Op, Value *V, Type *DestTy, const Twine &Name="")
Definition: IRBuilder.h:1190
void addIncoming(Value *V, BasicBlock *BB)
Value * CreateICmp(CmpInst::Predicate P, Value *LHS, Value *RHS, const Twine &Name="")
Definition: IRBuilder.h:1321
unsigned getNumOperands() const
Definition: User.h:110
Value * CreateBinOp(Instruction::BinaryOps Opc, Value *LHS, Value *RHS, const Twine &Name="", MDNode *FPMathTag=nullptr)
Definition: IRBuilder.h:851
bool isUnconditional() const
bool isIdenticalTo(const Instruction *I) const
static Constant * get(unsigned Opcode, Constant *C1, Constant *C2, unsigned Flags=0)
Definition: Constants.cpp:1752
Base class of casting instructions.
Definition: InstrTypes.h:387
Value * CreateFCmp(CmpInst::Predicate P, Value *LHS, Value *RHS, const Twine &Name="")
Definition: IRBuilder.h:1328
void takeName(Value *V)
Definition: Value.cpp:242
void SetInsertPoint(BasicBlock *TheBB)
This specifies that created instructions should be appended to the end of the specified block...
Definition: IRBuilder.h:83
unsigned getNumIncomingValues() const
LLVM Basic Block Representation.
Definition: BasicBlock.h:72
LLVM Constant Representation.
Definition: Constant.h:41
Instruction * ReplaceInstUsesWith(Instruction &I, Value *V)
Definition: InstCombine.h:282
BasicBlock * getIncomingBlock(unsigned i) const
Value * getOperand(unsigned i) const
Definition: User.h:90
BuilderTy * Builder
Definition: InstCombine.h:102
Value * getIncomingValue(unsigned i) const
Value * DoPHITranslation(const BasicBlock *CurBB, const BasicBlock *PredBB)
Definition: Value.cpp:607
Type * getType() const
Definition: Value.h:215
static PHINode * Create(Type *Ty, unsigned NumReservedValues, const Twine &NameStr="", Instruction *InsertBefore=nullptr)
bool isNullValue() const
Definition: Constants.cpp:75
Value * CreateSelect(Value *C, Value *True, Value *False, const Twine &Name="")
Definition: IRBuilder.h:1377
iterator_range< user_iterator > users()
Definition: Value.h:273
LLVM_ATTRIBUTE_UNUSED_RESULT std::enable_if< !is_simple_type< Y >::value, typename cast_retty< X, const Y >::ret_type >::type dyn_cast(const Y &Val)
Definition: Casting.h:265
#define I(x, y, z)
Definition: MD5.cpp:54
TerminatorInst * getTerminator()
Returns the terminator instruction if the block is well formed or null if the block is not well forme...
Definition: BasicBlock.cpp:124
bool hasOneUse() const
Definition: Value.h:284
Instruction * InsertNewInstBefore(Instruction *New, Instruction &Old)
Definition: InstCombine.h:259
user_iterator user_begin()
Definition: Value.h:267
LLVM Value Representation.
Definition: Value.h:69
unsigned getOpcode() const
getOpcode() returns a member of one of the enums like Instruction::Add.
Definition: Instruction.h:86
Instruction * EraseInstFromFunction(Instruction &I)
Definition: InstCombine.h:301
static Constant * getCompare(unsigned short pred, Constant *C1, Constant *C2)
Return an ICmp or FCmp comparison operator constant expression.
Definition: Constants.cpp:1871
static Constant * getCast(unsigned ops, Constant *C, Type *Ty)
Definition: Constants.cpp:1493
const BasicBlock * getParent() const
Definition: Instruction.h:53
user_iterator user_end()
Definition: Value.h:269
Instruction * InstCombiner::FoldOpIntoSelect ( Instruction Op,
SelectInst SI 
)
private

Definition at line 687 of file InstructionCombining.cpp.

687  {
688  // Don't modify shared select instructions
689  if (!SI->hasOneUse()) return nullptr;
690  Value *TV = SI->getOperand(1);
691  Value *FV = SI->getOperand(2);
692 
693  if (isa<Constant>(TV) || isa<Constant>(FV)) {
694  // Bool selects with constant operands can be folded to logical ops.
695  if (SI->getType()->isIntegerTy(1)) return nullptr;
696 
697  // If it's a bitcast involving vectors, make sure it has the same number of
698  // elements on both sides.
699  if (BitCastInst *BC = dyn_cast<BitCastInst>(&Op)) {
700  VectorType *DestTy = dyn_cast<VectorType>(BC->getDestTy());
701  VectorType *SrcTy = dyn_cast<VectorType>(BC->getSrcTy());
702 
703  // Verify that either both or neither are vectors.
704  if ((SrcTy == nullptr) != (DestTy == nullptr)) return nullptr;
705  // If vectors, verify that they have the same number of elements.
706  if (SrcTy && SrcTy->getNumElements() != DestTy->getNumElements())
707  return nullptr;
708  }
709 
710  Value *SelectTrueVal = FoldOperationIntoSelectOperand(Op, TV, this);
711  Value *SelectFalseVal = FoldOperationIntoSelectOperand(Op, FV, this);
712 
713  return SelectInst::Create(SI->getCondition(),
714  SelectTrueVal, SelectFalseVal);
715  }
716  return nullptr;
717 }
static SelectInst * Create(Value *C, Value *S1, Value *S2, const Twine &NameStr="", Instruction *InsertBefore=nullptr)
This class represents a no-op cast from one type to another.
unsigned getNumElements() const
Return the number of elements in the Vector type.
Definition: DerivedTypes.h:428
const Value * getCondition() const
Value * getOperand(unsigned i) const
Definition: User.h:90
static Value * FoldOperationIntoSelectOperand(Instruction &I, Value *SO, InstCombiner *IC)
Type * getType() const
Definition: Value.h:215
bool isIntegerTy() const
Definition: Type.h:193
LLVM_ATTRIBUTE_UNUSED_RESULT std::enable_if< !is_simple_type< Y >::value, typename cast_retty< X, const Y >::ret_type >::type dyn_cast(const Y &Val)
Definition: Casting.h:265
bool hasOneUse() const
Definition: Value.h:284
LLVM Value Representation.
Definition: Value.h:69
Value * InstCombiner::FoldOrOfFCmps ( FCmpInst LHS,
FCmpInst RHS 
)

FoldOrOfFCmps - Optimize (fcmp)|(fcmp). NOTE: Unlike the rest of instcombine, this returns a Value which should already be inserted into the function.

Definition at line 1823 of file InstCombineAndOrXor.cpp.

1823  {
1824  if (LHS->getPredicate() == FCmpInst::FCMP_UNO &&
1825  RHS->getPredicate() == FCmpInst::FCMP_UNO &&
1826  LHS->getOperand(0)->getType() == RHS->getOperand(0)->getType()) {
1827  if (ConstantFP *LHSC = dyn_cast<ConstantFP>(LHS->getOperand(1)))
1828  if (ConstantFP *RHSC = dyn_cast<ConstantFP>(RHS->getOperand(1))) {
1829  // If either of the constants are nans, then the whole thing returns
1830  // true.
1831  if (LHSC->getValueAPF().isNaN() || RHSC->getValueAPF().isNaN())
1832  return Builder->getTrue();
1833 
1834  // Otherwise, no need to compare the two constants, compare the
1835  // rest.
1836  return Builder->CreateFCmpUNO(LHS->getOperand(0), RHS->getOperand(0));
1837  }
1838 
1839  // Handle vector zeros. This occurs because the canonical form of
1840  // "fcmp uno x,x" is "fcmp uno x, 0".
1841  if (isa<ConstantAggregateZero>(LHS->getOperand(1)) &&
1842  isa<ConstantAggregateZero>(RHS->getOperand(1)))
1843  return Builder->CreateFCmpUNO(LHS->getOperand(0), RHS->getOperand(0));
1844 
1845  return nullptr;
1846  }
1847 
1848  Value *Op0LHS = LHS->getOperand(0), *Op0RHS = LHS->getOperand(1);
1849  Value *Op1LHS = RHS->getOperand(0), *Op1RHS = RHS->getOperand(1);
1850  FCmpInst::Predicate Op0CC = LHS->getPredicate(), Op1CC = RHS->getPredicate();
1851 
1852  if (Op0LHS == Op1RHS && Op0RHS == Op1LHS) {
1853  // Swap RHS operands to match LHS.
1854  Op1CC = FCmpInst::getSwappedPredicate(Op1CC);
1855  std::swap(Op1LHS, Op1RHS);
1856  }
1857  if (Op0LHS == Op1LHS && Op0RHS == Op1RHS) {
1858  // Simplify (fcmp cc0 x, y) | (fcmp cc1 x, y).
1859  if (Op0CC == Op1CC)
1860  return Builder->CreateFCmp((FCmpInst::Predicate)Op0CC, Op0LHS, Op0RHS);
1861  if (Op0CC == FCmpInst::FCMP_TRUE || Op1CC == FCmpInst::FCMP_TRUE)
1863  if (Op0CC == FCmpInst::FCMP_FALSE)
1864  return RHS;
1865  if (Op1CC == FCmpInst::FCMP_FALSE)
1866  return LHS;
1867  bool Op0Ordered;
1868  bool Op1Ordered;
1869  unsigned Op0Pred = getFCmpCode(Op0CC, Op0Ordered);
1870  unsigned Op1Pred = getFCmpCode(Op1CC, Op1Ordered);
1871  if (Op0Ordered == Op1Ordered) {
1872  // If both are ordered or unordered, return a new fcmp with
1873  // or'ed predicates.
1874  return getFCmpValue(Op0Ordered, Op0Pred|Op1Pred, Op0LHS, Op0RHS, Builder);
1875  }
1876  }
1877  return nullptr;
1878 }
static Type * makeCmpResultType(Type *opnd_type)
Create a result type for fcmp/icmp.
Definition: InstrTypes.h:853
1 0 0 0 True if unordered: isnan(X) | isnan(Y)
Definition: InstrTypes.h:682
Value * CreateFCmp(CmpInst::Predicate P, Value *LHS, Value *RHS, const Twine &Name="")
Definition: IRBuilder.h:1328
static Value * getFCmpValue(bool isordered, unsigned code, Value *LHS, Value *RHS, InstCombiner::BuilderTy *Builder)
ConstantInt * getTrue()
Get the constant value for i1 true.
Definition: IRBuilder.h:256
static unsigned getFCmpCode(FCmpInst::Predicate CC, bool &isOrdered)
Value * getOperand(unsigned i) const
Definition: User.h:90
Predicate getPredicate() const
Return the predicate for this instruction.
Definition: InstrTypes.h:735
1 1 1 1 Always true (always folded)
Definition: InstrTypes.h:689
BuilderTy * Builder
Definition: InstCombine.h:102
Type * getType() const
Definition: Value.h:215
Predicate getSwappedPredicate() const
Return the predicate as if the operands were swapped.
Definition: InstrTypes.h:774
static Constant * get(Type *Ty, uint64_t V, bool isSigned=false)
Definition: Constants.cpp:528
void swap(llvm::BitVector &LHS, llvm::BitVector &RHS)
Implement std::swap in terms of BitVector swap.
Definition: BitVector.h:590
Value * CreateFCmpUNO(Value *LHS, Value *RHS, const Twine &Name="")
Definition: IRBuilder.h:1299
LLVM Value Representation.
Definition: Value.h:69
0 0 0 0 Always false (always folded)
Definition: InstrTypes.h:674
Value * InstCombiner::FoldOrOfICmps ( ICmpInst LHS,
ICmpInst RHS 
)

FoldOrOfICmps - Fold (icmp)|(icmp) if possible.

Definition at line 1557 of file InstCombineAndOrXor.cpp.

1557  {
1558  ICmpInst::Predicate LHSCC = LHS->getPredicate(), RHSCC = RHS->getPredicate();
1559 
1560  // Fold (iszero(A & K1) | iszero(A & K2)) -> (A & (K1 | K2)) != (K1 | K2)
1561  // if K1 and K2 are a one-bit mask.
1562  ConstantInt *LHSCst = dyn_cast<ConstantInt>(LHS->getOperand(1));
1563  ConstantInt *RHSCst = dyn_cast<ConstantInt>(RHS->getOperand(1));
1564 
1565  if (LHS->getPredicate() == ICmpInst::ICMP_EQ && LHSCst && LHSCst->isZero() &&
1566  RHS->getPredicate() == ICmpInst::ICMP_EQ && RHSCst && RHSCst->isZero()) {
1567 
1570  if (LAnd && RAnd && LAnd->hasOneUse() && RHS->hasOneUse() &&
1571  LAnd->getOpcode() == Instruction::And &&
1572  RAnd->getOpcode() == Instruction::And) {
1573 
1574  Value *Mask = nullptr;
1575  Value *Masked = nullptr;
1576  if (LAnd->getOperand(0) == RAnd->getOperand(0) &&
1577  isKnownToBeAPowerOfTwo(LAnd->getOperand(1)) &&
1578  isKnownToBeAPowerOfTwo(RAnd->getOperand(1))) {
1579  Mask = Builder->CreateOr(LAnd->getOperand(1), RAnd->getOperand(1));
1580  Masked = Builder->CreateAnd(LAnd->getOperand(0), Mask);
1581  } else if (LAnd->getOperand(1) == RAnd->getOperand(1) &&
1582  isKnownToBeAPowerOfTwo(LAnd->getOperand(0)) &&
1583  isKnownToBeAPowerOfTwo(RAnd->getOperand(0))) {
1584  Mask = Builder->CreateOr(LAnd->getOperand(0), RAnd->getOperand(0));
1585  Masked = Builder->CreateAnd(LAnd->getOperand(1), Mask);
1586  }
1587 
1588  if (Masked)
1589  return Builder->CreateICmp(ICmpInst::ICMP_NE, Masked, Mask);
1590  }
1591  }
1592 
1593  // (icmp1 A, B) | (icmp2 A, B) --> (icmp3 A, B)
1594  if (PredicatesFoldable(LHSCC, RHSCC)) {
1595  if (LHS->getOperand(0) == RHS->getOperand(1) &&
1596  LHS->getOperand(1) == RHS->getOperand(0))
1597  LHS->swapOperands();
1598  if (LHS->getOperand(0) == RHS->getOperand(0) &&
1599  LHS->getOperand(1) == RHS->getOperand(1)) {
1600  Value *Op0 = LHS->getOperand(0), *Op1 = LHS->getOperand(1);
1601  unsigned Code = getICmpCode(LHS) | getICmpCode(RHS);
1602  bool isSigned = LHS->isSigned() || RHS->isSigned();
1603  return getNewICmpValue(isSigned, Code, Op0, Op1, Builder);
1604  }
1605  }
1606 
1607  // handle (roughly):
1608  // (icmp ne (A & B), C) | (icmp ne (A & D), E)
1609  if (Value *V = foldLogOpOfMaskedICmps(LHS, RHS, false, Builder))
1610  return V;
1611 
1612  Value *Val = LHS->getOperand(0), *Val2 = RHS->getOperand(0);
1613  if (LHS->hasOneUse() || RHS->hasOneUse()) {
1614  // (icmp eq B, 0) | (icmp ult A, B) -> (icmp ule A, B-1)
1615  // (icmp eq B, 0) | (icmp ugt B, A) -> (icmp ule A, B-1)
1616  Value *A = nullptr, *B = nullptr;
1617  if (LHSCC == ICmpInst::ICMP_EQ && LHSCst && LHSCst->isZero()) {
1618  B = Val;
1619  if (RHSCC == ICmpInst::ICMP_ULT && Val == RHS->getOperand(1))
1620  A = Val2;
1621  else if (RHSCC == ICmpInst::ICMP_UGT && Val == Val2)
1622  A = RHS->getOperand(1);
1623  }
1624  // (icmp ult A, B) | (icmp eq B, 0) -> (icmp ule A, B-1)
1625  // (icmp ugt B, A) | (icmp eq B, 0) -> (icmp ule A, B-1)
1626  else if (RHSCC == ICmpInst::ICMP_EQ && RHSCst && RHSCst->isZero()) {
1627  B = Val2;
1628  if (LHSCC == ICmpInst::ICMP_ULT && Val2 == LHS->getOperand(1))
1629  A = Val;
1630  else if (LHSCC == ICmpInst::ICMP_UGT && Val2 == Val)
1631  A = LHS->getOperand(1);
1632  }
1633  if (A && B)
1634  return Builder->CreateICmp(
1636  Builder->CreateAdd(B, ConstantInt::getSigned(B->getType(), -1)), A);
1637  }
1638 
1639  // This only handles icmp of constants: (icmp1 A, C1) | (icmp2 B, C2).
1640  if (!LHSCst || !RHSCst) return nullptr;
1641 
1642  if (LHSCst == RHSCst && LHSCC == RHSCC) {
1643  // (icmp ne A, 0) | (icmp ne B, 0) --> (icmp ne (A|B), 0)
1644  if (LHSCC == ICmpInst::ICMP_NE && LHSCst->isZero()) {
1645  Value *NewOr = Builder->CreateOr(Val, Val2);
1646  return Builder->CreateICmp(LHSCC, NewOr, LHSCst);
1647  }
1648  }
1649 
1650  // (icmp ult (X + CA), C1) | (icmp eq X, C2) -> (icmp ule (X + CA), C1)
1651  // iff C2 + CA == C1.
1652  if (LHSCC == ICmpInst::ICMP_ULT && RHSCC == ICmpInst::ICMP_EQ) {
1653  ConstantInt *AddCst;
1654  if (match(Val, m_Add(m_Specific(Val2), m_ConstantInt(AddCst))))
1655  if (RHSCst->getValue() + AddCst->getValue() == LHSCst->getValue())
1656  return Builder->CreateICmpULE(Val, LHSCst);
1657  }
1658 
1659  // From here on, we only handle:
1660  // (icmp1 A, C1) | (icmp2 A, C2) --> something simpler.
1661  if (Val != Val2) return nullptr;
1662 
1663  // ICMP_[US][GL]E X, CST is folded to ICMP_[US][GL]T elsewhere.
1664  if (LHSCC == ICmpInst::ICMP_UGE || LHSCC == ICmpInst::ICMP_ULE ||
1665  RHSCC == ICmpInst::ICMP_UGE || RHSCC == ICmpInst::ICMP_ULE ||
1666  LHSCC == ICmpInst::ICMP_SGE || LHSCC == ICmpInst::ICMP_SLE ||
1667  RHSCC == ICmpInst::ICMP_SGE || RHSCC == ICmpInst::ICMP_SLE)
1668  return nullptr;
1669 
1670  // We can't fold (ugt x, C) | (sgt x, C2).
1671  if (!PredicatesFoldable(LHSCC, RHSCC))
1672  return nullptr;
1673 
1674  // Ensure that the larger constant is on the RHS.
1675  bool ShouldSwap;
1676  if (CmpInst::isSigned(LHSCC) ||
1677  (ICmpInst::isEquality(LHSCC) &&
1678  CmpInst::isSigned(RHSCC)))
1679  ShouldSwap = LHSCst->getValue().sgt(RHSCst->getValue());
1680  else
1681  ShouldSwap = LHSCst->getValue().ugt(RHSCst->getValue());
1682 
1683  if (ShouldSwap) {
1684  std::swap(LHS, RHS);
1685  std::swap(LHSCst, RHSCst);
1686  std::swap(LHSCC, RHSCC);
1687  }
1688 
1689  // At this point, we know we have two icmp instructions
1690  // comparing a value against two constants and or'ing the result
1691  // together. Because of the above check, we know that we only have
1692  // ICMP_EQ, ICMP_NE, ICMP_LT, and ICMP_GT here. We also know (from the
1693  // icmp folding check above), that the two constants are not
1694  // equal.
1695  assert(LHSCst != RHSCst && "Compares not folded above?");
1696 
1697  switch (LHSCC) {
1698  default: llvm_unreachable("Unknown integer condition code!");
1699  case ICmpInst::ICMP_EQ:
1700  switch (RHSCC) {
1701  default: llvm_unreachable("Unknown integer condition code!");
1702  case ICmpInst::ICMP_EQ:
1703  if (LHS->getOperand(0) == RHS->getOperand(0)) {
1704  // if LHSCst and RHSCst differ only by one bit:
1705  // (A == C1 || A == C2) -> (A & ~(C1 ^ C2)) == C1
1706  assert(LHSCst->getValue().ule(LHSCst->getValue()));
1707 
1708  APInt Xor = LHSCst->getValue() ^ RHSCst->getValue();
1709  if (Xor.isPowerOf2()) {
1710  Value *NegCst = Builder->getInt(~Xor);
1711  Value *And = Builder->CreateAnd(LHS->getOperand(0), NegCst);
1712  return Builder->CreateICmp(ICmpInst::ICMP_EQ, And, LHSCst);
1713  }
1714  }
1715 
1716  if (LHSCst == SubOne(RHSCst)) {
1717  // (X == 13 | X == 14) -> X-13 <u 2
1718  Constant *AddCST = ConstantExpr::getNeg(LHSCst);
1719  Value *Add = Builder->CreateAdd(Val, AddCST, Val->getName()+".off");
1720  AddCST = ConstantExpr::getSub(AddOne(RHSCst), LHSCst);
1721  return Builder->CreateICmpULT(Add, AddCST);
1722  }
1723 
1724  break; // (X == 13 | X == 15) -> no change
1725  case ICmpInst::ICMP_UGT: // (X == 13 | X u> 14) -> no change
1726  case ICmpInst::ICMP_SGT: // (X == 13 | X s> 14) -> no change
1727  break;
1728  case ICmpInst::ICMP_NE: // (X == 13 | X != 15) -> X != 15
1729  case ICmpInst::ICMP_ULT: // (X == 13 | X u< 15) -> X u< 15
1730  case ICmpInst::ICMP_SLT: // (X == 13 | X s< 15) -> X s< 15
1731  return RHS;
1732  }
1733  break;
1734  case ICmpInst::ICMP_NE:
1735  switch (RHSCC) {
1736  default: llvm_unreachable("Unknown integer condition code!");
1737  case ICmpInst::ICMP_EQ: // (X != 13 | X == 15) -> X != 13
1738  case ICmpInst::ICMP_UGT: // (X != 13 | X u> 15) -> X != 13
1739  case ICmpInst::ICMP_SGT: // (X != 13 | X s> 15) -> X != 13
1740  return LHS;
1741  case ICmpInst::ICMP_NE: // (X != 13 | X != 15) -> true
1742  case ICmpInst::ICMP_ULT: // (X != 13 | X u< 15) -> true
1743  case ICmpInst::ICMP_SLT: // (X != 13 | X s< 15) -> true
1744  return Builder->getTrue();
1745  }
1746  case ICmpInst::ICMP_ULT:
1747  switch (RHSCC) {
1748  default: llvm_unreachable("Unknown integer condition code!");
1749  case ICmpInst::ICMP_EQ: // (X u< 13 | X == 14) -> no change
1750  break;
1751  case ICmpInst::ICMP_UGT: // (X u< 13 | X u> 15) -> (X-13) u> 2
1752  // If RHSCst is [us]MAXINT, it is always false. Not handling
1753  // this can cause overflow.
1754  if (RHSCst->isMaxValue(false))
1755  return LHS;
1756  return InsertRangeTest(Val, LHSCst, AddOne(RHSCst), false, false);
1757  case ICmpInst::ICMP_SGT: // (X u< 13 | X s> 15) -> no change
1758  break;
1759  case ICmpInst::ICMP_NE: // (X u< 13 | X != 15) -> X != 15
1760  case ICmpInst::ICMP_ULT: // (X u< 13 | X u< 15) -> X u< 15
1761  return RHS;
1762  case ICmpInst::ICMP_SLT: // (X u< 13 | X s< 15) -> no change
1763  break;
1764  }
1765  break;
1766  case ICmpInst::ICMP_SLT:
1767  switch (RHSCC) {
1768  default: llvm_unreachable("Unknown integer condition code!");
1769  case ICmpInst::ICMP_EQ: // (X s< 13 | X == 14) -> no change
1770  break;
1771  case ICmpInst::ICMP_SGT: // (X s< 13 | X s> 15) -> (X-13) s> 2
1772  // If RHSCst is [us]MAXINT, it is always false. Not handling
1773  // this can cause overflow.
1774  if (RHSCst->isMaxValue(true))
1775  return LHS;
1776  return InsertRangeTest(Val, LHSCst, AddOne(RHSCst), true, false);
1777  case ICmpInst::ICMP_UGT: // (X s< 13 | X u> 15) -> no change
1778  break;
1779  case ICmpInst::ICMP_NE: // (X s< 13 | X != 15) -> X != 15
1780  case ICmpInst::ICMP_SLT: // (X s< 13 | X s< 15) -> X s< 15
1781  return RHS;
1782  case ICmpInst::ICMP_ULT: // (X s< 13 | X u< 15) -> no change
1783  break;
1784  }
1785  break;
1786  case ICmpInst::ICMP_UGT:
1787  switch (RHSCC) {
1788  default: llvm_unreachable("Unknown integer condition code!");
1789  case ICmpInst::ICMP_EQ: // (X u> 13 | X == 15) -> X u> 13
1790  case ICmpInst::ICMP_UGT: // (X u> 13 | X u> 15) -> X u> 13
1791  return LHS;
1792  case ICmpInst::ICMP_SGT: // (X u> 13 | X s> 15) -> no change
1793  break;
1794  case ICmpInst::ICMP_NE: // (X u> 13 | X != 15) -> true
1795  case ICmpInst::ICMP_ULT: // (X u> 13 | X u< 15) -> true
1796  return Builder->getTrue();
1797  case ICmpInst::ICMP_SLT: // (X u> 13 | X s< 15) -> no change
1798  break;
1799  }
1800  break;
1801  case ICmpInst::ICMP_SGT:
1802  switch (RHSCC) {
1803  default: llvm_unreachable("Unknown integer condition code!");
1804  case ICmpInst::ICMP_EQ: // (X s> 13 | X == 15) -> X > 13
1805  case ICmpInst::ICMP_SGT: // (X s> 13 | X s> 15) -> X > 13
1806  return LHS;
1807  case ICmpInst::ICMP_UGT: // (X s> 13 | X u> 15) -> no change
1808  break;
1809  case ICmpInst::ICMP_NE: // (X s> 13 | X != 15) -> true
1810  case ICmpInst::ICMP_SLT: // (X s> 13 | X s< 15) -> true
1811  return Builder->getTrue();
1812  case ICmpInst::ICMP_ULT: // (X s> 13 | X u< 15) -> no change
1813  break;
1814  }
1815  break;
1816  }
1817  return nullptr;
1818 }
Value * CreateICmp(CmpInst::Predicate P, Value *LHS, Value *RHS, const Twine &Name="")
Definition: IRBuilder.h:1321
static Value * foldLogOpOfMaskedICmps(ICmpInst *LHS, ICmpInst *RHS, bool IsAnd, llvm::InstCombiner::BuilderTy *Builder)
unsigned getICmpCode(const ICmpInst *ICI, bool InvertPred=false)
unsigned less or equal
Definition: InstrTypes.h:698
unsigned less than
Definition: InstrTypes.h:697
bool isSigned() const
Determine if this instruction is using a signed comparison.
Definition: InstrTypes.h:801
Value * CreateICmpULT(Value *LHS, Value *RHS, const Twine &Name="")
Definition: IRBuilder.h:1259
static Constant * getSub(Constant *C1, Constant *C2, bool HasNUW=false, bool HasNSW=false)
Definition: Constants.cpp:2113
StringRef getName() const
Definition: Value.cpp:168
Value * CreateICmpULE(Value *LHS, Value *RHS, const Twine &Name="")
Definition: IRBuilder.h:1262
bool match(Val *V, const Pattern &P)
Definition: PatternMatch.h:42
const APInt & getValue() const
Return the constant's value.
Definition: Constants.h:105
#define llvm_unreachable(msg)
Definition: ErrorHandling.h:98
static Constant * AddOne(Constant *C)
AddOne - Add one to a Constant.
Definition: InstCombine.h:60
BinaryOp_match< LHS, RHS, Instruction::Add > m_Add(const LHS &L, const RHS &R)
Definition: PatternMatch.h:395
Value * CreateAnd(Value *LHS, Value *RHS, const Twine &Name="")
Definition: IRBuilder.h:806
Value * CreateOr(Value *LHS, Value *RHS, const Twine &Name="")
Definition: IRBuilder.h:822
bool sgt(const APInt &RHS) const
Signed greather than comparison.
Definition: APInt.h:1096
class_match< ConstantInt > m_ConstantInt()
m_ConstantInt() - Match an arbitrary ConstantInt and ignore it.
Definition: PatternMatch.h:72
assert(Globals.size() > 1)
ConstantInt * getTrue()
Get the constant value for i1 true.
Definition: IRBuilder.h:256
bool isEquality() const
Value * CreateAdd(Value *LHS, Value *RHS, const Twine &Name="", bool HasNUW=false, bool HasNSW=false)
Definition: IRBuilder.h:632
LLVM Constant Representation.
Definition: Constant.h:41
APInt Xor(const APInt &LHS, const APInt &RHS)
Bitwise XOR function for APInt.
Definition: APInt.h:1875
specificval_ty m_Specific(const Value *V)
m_Specific - Match if we have a specific specified value.
Definition: PatternMatch.h:323
Value * InsertRangeTest(Value *V, Constant *Lo, Constant *Hi, bool isSigned, bool Inside)
Value * getOperand(unsigned i) const
Definition: User.h:90
Predicate getPredicate() const
Return the predicate for this instruction.
Definition: InstrTypes.h:735
BuilderTy * Builder
Definition: InstCombine.h:102
void swapOperands()
Swap operands and adjust predicate.
bool isPowerOf2() const
Check if this APInt's value is a power of two greater than zero.
Definition: APInt.h:388
signed greater than
Definition: InstrTypes.h:699
bool ugt(const APInt &RHS) const
Unsigned greather than comparison.
Definition: APInt.h:1080
BinaryOps getOpcode() const
Definition: InstrTypes.h:326
Definition: test.h:1
Class for constant integers.
Definition: Constants.h:51
signed less than
Definition: InstrTypes.h:701
bool isKnownToBeAPowerOfTwo(Value *V, bool OrZero=false, unsigned Depth=0)
static ConstantInt * getSigned(IntegerType *Ty, int64_t V)
Get a ConstantInt for a specific signed value.
Definition: Constants.cpp:543
bool isZero() const
Definition: Constants.h:160
bool PredicatesFoldable(CmpInst::Predicate p1, CmpInst::Predicate p2)
void swap(llvm::BitVector &LHS, llvm::BitVector &RHS)
Implement std::swap in terms of BitVector swap.
Definition: BitVector.h:590
signed less or equal
Definition: InstrTypes.h:702
Class for arbitrary precision integers.
Definition: APInt.h:75
LLVM_ATTRIBUTE_UNUSED_RESULT std::enable_if< !is_simple_type< Y >::value, typename cast_retty< X, const Y >::ret_type >::type dyn_cast(const Y &Val)
Definition: Casting.h:265
APInt And(const APInt &LHS, const APInt &RHS)
Bitwise AND function for APInt.
Definition: APInt.h:1865
static Constant * getNeg(Constant *C, bool HasNUW=false, bool HasNSW=false)
Definition: Constants.cpp:2083
unsigned greater or equal
Definition: InstrTypes.h:696
bool hasOneUse() const
Definition: Value.h:284
LLVM Value Representation.
Definition: Value.h:69
static Value * getNewICmpValue(bool Sign, unsigned Code, Value *LHS, Value *RHS, InstCombiner::BuilderTy *Builder)
unsigned greater than
Definition: InstrTypes.h:695
ConstantInt * getInt(const APInt &AI)
Get a constant integer value.
Definition: IRBuilder.h:292
bool ule(const APInt &RHS) const
Unsigned less or equal comparison.
Definition: APInt.h:1048
static Constant * SubOne(Constant *C)
SubOne - Subtract one from a Constant.
Definition: InstCombine.h:64
signed greater or equal
Definition: InstrTypes.h:700
Instruction * InstCombiner::FoldOrWithConstants ( BinaryOperator I,
Value Op,
Value A,
Value B,
Value C 
)

FoldOrWithConstants - This helper function folds:

((A | B) & C1) | (B & C2)

into:

(A & C1) | B

when the XOR of the two constants is "all ones" (-1).

Definition at line 1889 of file InstCombineAndOrXor.cpp.

1890  {
1891  ConstantInt *CI1 = dyn_cast<ConstantInt>(C);
1892  if (!CI1) return nullptr;
1893 
1894  Value *V1 = nullptr;
1895  ConstantInt *CI2 = nullptr;
1896  if (!match(Op, m_And(m_Value(V1), m_ConstantInt(CI2)))) return nullptr;
1897 
1898  APInt Xor = CI1->getValue() ^ CI2->getValue();
1899  if (!Xor.isAllOnesValue()) return nullptr;
1900 
1901  if (V1 == A || V1 == B) {
1902  Value *NewOp = Builder->CreateAnd((V1 == A) ? B : A, CI1);
1903  return BinaryOperator::CreateOr(NewOp, V1);
1904  }
1905 
1906  return nullptr;
1907 }
BinaryOp_match< LHS, RHS, Instruction::And > m_And(const LHS &L, const RHS &R)
Definition: PatternMatch.h:467
class_match< Value > m_Value()
m_Value() - Match an arbitrary value and ignore it.
Definition: PatternMatch.h:70
bool match(Val *V, const Pattern &P)
Definition: PatternMatch.h:42
const APInt & getValue() const
Return the constant's value.
Definition: Constants.h:105
Value * CreateAnd(Value *LHS, Value *RHS, const Twine &Name="")
Definition: IRBuilder.h:806
class_match< ConstantInt > m_ConstantInt()
m_ConstantInt() - Match an arbitrary ConstantInt and ignore it.
Definition: PatternMatch.h:72
APInt Xor(const APInt &LHS, const APInt &RHS)
Bitwise XOR function for APInt.
Definition: APInt.h:1875
BuilderTy * Builder
Definition: InstCombine.h:102
Class for constant integers.
Definition: Constants.h:51
Class for arbitrary precision integers.
Definition: APInt.h:75
LLVM_ATTRIBUTE_UNUSED_RESULT std::enable_if< !is_simple_type< Y >::value, typename cast_retty< X, const Y >::ret_type >::type dyn_cast(const Y &Val)
Definition: Casting.h:265
bool isAllOnesValue() const
Determine if all bits are set.
Definition: APInt.h:338
LLVM Value Representation.
Definition: Value.h:69
Instruction * InstCombiner::FoldPHIArgBinOpIntoPHI ( PHINode PN)
private

FoldPHIArgBinOpIntoPHI - If we have something like phi [add (a,b), add(a,c)] and if a/b/c and the add's all have a single use, turn this into a phi and a single binop.

Definition at line 26 of file InstCombinePHI.cpp.

26  {
27  Instruction *FirstInst = cast<Instruction>(PN.getIncomingValue(0));
28  assert(isa<BinaryOperator>(FirstInst) || isa<CmpInst>(FirstInst));
29  unsigned Opc = FirstInst->getOpcode();
30  Value *LHSVal = FirstInst->getOperand(0);
31  Value *RHSVal = FirstInst->getOperand(1);
32 
33  Type *LHSType = LHSVal->getType();
34  Type *RHSType = RHSVal->getType();
35 
36  bool isNUW = false, isNSW = false, isExact = false;
38  dyn_cast<OverflowingBinaryOperator>(FirstInst)) {
39  isNUW = BO->hasNoUnsignedWrap();
40  isNSW = BO->hasNoSignedWrap();
41  } else if (PossiblyExactOperator *PEO =
42  dyn_cast<PossiblyExactOperator>(FirstInst))
43  isExact = PEO->isExact();
44 
45  // Scan to see if all operands are the same opcode, and all have one use.
46  for (unsigned i = 1; i != PN.getNumIncomingValues(); ++i) {
48  if (!I || I->getOpcode() != Opc || !I->hasOneUse() ||
49  // Verify type of the LHS matches so we don't fold cmp's of different
50  // types.
51  I->getOperand(0)->getType() != LHSType ||
52  I->getOperand(1)->getType() != RHSType)
53  return nullptr;
54 
55  // If they are CmpInst instructions, check their predicates
56  if (CmpInst *CI = dyn_cast<CmpInst>(I))
57  if (CI->getPredicate() != cast<CmpInst>(FirstInst)->getPredicate())
58  return nullptr;
59 
60  if (isNUW)
61  isNUW = cast<OverflowingBinaryOperator>(I)->hasNoUnsignedWrap();
62  if (isNSW)
63  isNSW = cast<OverflowingBinaryOperator>(I)->hasNoSignedWrap();
64  if (isExact)
65  isExact = cast<PossiblyExactOperator>(I)->isExact();
66 
67  // Keep track of which operand needs a phi node.
68  if (I->getOperand(0) != LHSVal) LHSVal = nullptr;
69  if (I->getOperand(1) != RHSVal) RHSVal = nullptr;
70  }
71 
72  // If both LHS and RHS would need a PHI, don't do this transformation,
73  // because it would increase the number of PHIs entering the block,
74  // which leads to higher register pressure. This is especially
75  // bad when the PHIs are in the header of a loop.
76  if (!LHSVal && !RHSVal)
77  return nullptr;
78 
79  // Otherwise, this is safe to transform!
80 
81  Value *InLHS = FirstInst->getOperand(0);
82  Value *InRHS = FirstInst->getOperand(1);
83  PHINode *NewLHS = nullptr, *NewRHS = nullptr;
84  if (!LHSVal) {
85  NewLHS = PHINode::Create(LHSType, PN.getNumIncomingValues(),
86  FirstInst->getOperand(0)->getName() + ".pn");
87  NewLHS->addIncoming(InLHS, PN.getIncomingBlock(0));
88  InsertNewInstBefore(NewLHS, PN);
89  LHSVal = NewLHS;
90  }
91 
92  if (!RHSVal) {
93  NewRHS = PHINode::Create(RHSType, PN.getNumIncomingValues(),
94  FirstInst->getOperand(1)->getName() + ".pn");
95  NewRHS->addIncoming(InRHS, PN.getIncomingBlock(0));
96  InsertNewInstBefore(NewRHS, PN);
97  RHSVal = NewRHS;
98  }
99 
100  // Add all operands to the new PHIs.
101  if (NewLHS || NewRHS) {
102  for (unsigned i = 1, e = PN.getNumIncomingValues(); i != e; ++i) {
103  Instruction *InInst = cast<Instruction>(PN.getIncomingValue(i));
104  if (NewLHS) {
105  Value *NewInLHS = InInst->getOperand(0);
106  NewLHS->addIncoming(NewInLHS, PN.getIncomingBlock(i));
107  }
108  if (NewRHS) {
109  Value *NewInRHS = InInst->getOperand(1);
110  NewRHS->addIncoming(NewInRHS, PN.getIncomingBlock(i));
111  }
112  }
113  }
114 
115  if (CmpInst *CIOp = dyn_cast<CmpInst>(FirstInst)) {
116  CmpInst *NewCI = CmpInst::Create(CIOp->getOpcode(), CIOp->getPredicate(),
117  LHSVal, RHSVal);
118  NewCI->setDebugLoc(FirstInst->getDebugLoc());
119  return NewCI;
120  }
121 
122  BinaryOperator *BinOp = cast<BinaryOperator>(FirstInst);
123  BinaryOperator *NewBinOp =
124  BinaryOperator::Create(BinOp->getOpcode(), LHSVal, RHSVal);
125  if (isNUW) NewBinOp->setHasNoUnsignedWrap();
126  if (isNSW) NewBinOp->setHasNoSignedWrap();
127  if (isExact) NewBinOp->setIsExact();
128  NewBinOp->setDebugLoc(FirstInst->getDebugLoc());
129  return NewBinOp;
130 }
Abstract base class of comparison instructions.
Definition: InstrTypes.h:654
void addIncoming(Value *V, BasicBlock *BB)
void setDebugLoc(const DebugLoc &Loc)
setDebugLoc - Set the debug location information for this instruction.
Definition: Instruction.h:193
StringRef getName() const
Definition: Value.cpp:168
void setHasNoUnsignedWrap(bool b=true)
unsigned getNumIncomingValues() const
assert(Globals.size() > 1)
static CmpInst * Create(OtherOps Op, unsigned short predicate, Value *S1, Value *S2, const Twine &Name="", Instruction *InsertBefore=nullptr)
Create a CmpInst.
const DebugLoc & getDebugLoc() const
getDebugLoc - Return the debug location for this node as a DebugLoc.
Definition: Instruction.h:196
BasicBlock * getIncomingBlock(unsigned i) const
Value * getOperand(unsigned i) const
Definition: User.h:90
BinaryOps getOpcode() const
Definition: InstrTypes.h:326
Value * getIncomingValue(unsigned i) const
Type * getType() const
Definition: Value.h:215
static PHINode * Create(Type *Ty, unsigned NumReservedValues, const Twine &NameStr="", Instruction *InsertBefore=nullptr)
static BinaryOperator * Create(BinaryOps Op, Value *S1, Value *S2, const Twine &Name=Twine(), Instruction *InsertBefore=nullptr)
LLVM_ATTRIBUTE_UNUSED_RESULT std::enable_if< !is_simple_type< Y >::value, typename cast_retty< X, const Y >::ret_type >::type dyn_cast(const Y &Val)
Definition: Casting.h:265
#define I(x, y, z)
Definition: MD5.cpp:54
bool hasOneUse() const
Definition: Value.h:284
Instruction * InsertNewInstBefore(Instruction *New, Instruction &Old)
Definition: InstCombine.h:259
LLVM Value Representation.
Definition: Value.h:69
unsigned getOpcode() const
getOpcode() returns a member of one of the enums like Instruction::Add.
Definition: Instruction.h:86
Instruction * InstCombiner::FoldPHIArgGEPIntoPHI ( PHINode PN)
private

Definition at line 132 of file InstCombinePHI.cpp.

132  {
133  GetElementPtrInst *FirstInst =cast<GetElementPtrInst>(PN.getIncomingValue(0));
134 
135  SmallVector<Value*, 16> FixedOperands(FirstInst->op_begin(),
136  FirstInst->op_end());
137  // This is true if all GEP bases are allocas and if all indices into them are
138  // constants.
139  bool AllBasePointersAreAllocas = true;
140 
141  // We don't want to replace this phi if the replacement would require
142  // more than one phi, which leads to higher register pressure. This is
143  // especially bad when the PHIs are in the header of a loop.
144  bool NeededPhi = false;
145 
146  bool AllInBounds = true;
147 
148  // Scan to see if all operands are the same opcode, and all have one use.
149  for (unsigned i = 1; i != PN.getNumIncomingValues(); ++i) {
151  if (!GEP || !GEP->hasOneUse() || GEP->getType() != FirstInst->getType() ||
152  GEP->getNumOperands() != FirstInst->getNumOperands())
153  return nullptr;
154 
155  AllInBounds &= GEP->isInBounds();
156 
157  // Keep track of whether or not all GEPs are of alloca pointers.
158  if (AllBasePointersAreAllocas &&
159  (!isa<AllocaInst>(GEP->getOperand(0)) ||
160  !GEP->hasAllConstantIndices()))
161  AllBasePointersAreAllocas = false;
162 
163  // Compare the operand lists.
164  for (unsigned op = 0, e = FirstInst->getNumOperands(); op != e; ++op) {
165  if (FirstInst->getOperand(op) == GEP->getOperand(op))
166  continue;
167 
168  // Don't merge two GEPs when two operands differ (introducing phi nodes)
169  // if one of the PHIs has a constant for the index. The index may be
170  // substantially cheaper to compute for the constants, so making it a
171  // variable index could pessimize the path. This also handles the case
172  // for struct indices, which must always be constant.
173  if (isa<ConstantInt>(FirstInst->getOperand(op)) ||
174  isa<ConstantInt>(GEP->getOperand(op)))
175  return nullptr;
176 
177  if (FirstInst->getOperand(op)->getType() !=GEP->getOperand(op)->getType())
178  return nullptr;
179 
180  // If we already needed a PHI for an earlier operand, and another operand
181  // also requires a PHI, we'd be introducing more PHIs than we're
182  // eliminating, which increases register pressure on entry to the PHI's
183  // block.
184  if (NeededPhi)
185  return nullptr;
186 
187  FixedOperands[op] = nullptr; // Needs a PHI.
188  NeededPhi = true;
189  }
190  }
191 
192  // If all of the base pointers of the PHI'd GEPs are from allocas, don't
193  // bother doing this transformation. At best, this will just save a bit of
194  // offset calculation, but all the predecessors will have to materialize the
195  // stack address into a register anyway. We'd actually rather *clone* the
196  // load up into the predecessors so that we have a load of a gep of an alloca,
197  // which can usually all be folded into the load.
198  if (AllBasePointersAreAllocas)
199  return nullptr;
200 
201  // Otherwise, this is safe to transform. Insert PHI nodes for each operand
202  // that is variable.
203  SmallVector<PHINode*, 16> OperandPhis(FixedOperands.size());
204 
205  bool HasAnyPHIs = false;
206  for (unsigned i = 0, e = FixedOperands.size(); i != e; ++i) {
207  if (FixedOperands[i]) continue; // operand doesn't need a phi.
208  Value *FirstOp = FirstInst->getOperand(i);
209  PHINode *NewPN = PHINode::Create(FirstOp->getType(), e,
210  FirstOp->getName()+".pn");
211  InsertNewInstBefore(NewPN, PN);
212 
213  NewPN->addIncoming(FirstOp, PN.getIncomingBlock(0));
214  OperandPhis[i] = NewPN;
215  FixedOperands[i] = NewPN;
216  HasAnyPHIs = true;
217  }
218 
219 
220  // Add all operands to the new PHIs.
221  if (HasAnyPHIs) {
222  for (unsigned i = 1, e = PN.getNumIncomingValues(); i != e; ++i) {
223  GetElementPtrInst *InGEP =cast<GetElementPtrInst>(PN.getIncomingValue(i));
224  BasicBlock *InBB = PN.getIncomingBlock(i);
225 
226  for (unsigned op = 0, e = OperandPhis.size(); op != e; ++op)
227  if (PHINode *OpPhi = OperandPhis[op])
228  OpPhi->addIncoming(InGEP->getOperand(op), InBB);
229  }
230  }
231 
232  Value *Base = FixedOperands[0];
233  GetElementPtrInst *NewGEP =
234  GetElementPtrInst::Create(Base, makeArrayRef(FixedOperands).slice(1));
235  if (AllInBounds) NewGEP->setIsInBounds();
236  NewGEP->setDebugLoc(FirstInst->getDebugLoc());
237  return NewGEP;
238 }
void addIncoming(Value *V, BasicBlock *BB)
unsigned getNumOperands() const
Definition: User.h:110
void setDebugLoc(const DebugLoc &Loc)
setDebugLoc - Set the debug location information for this instruction.
Definition: Instruction.h:193
op_iterator op_begin()
Definition: User.h:120
StringRef getName() const
Definition: Value.cpp:168
ArrayRef< T > makeArrayRef(const T &OneElt)
Construct an ArrayRef from a single element.
Definition: ArrayRef.h:276
void setIsInBounds(bool b=true)
bool isInBounds() const
isInBounds - Determine whether the GEP has the inbounds flag.
unsigned getNumIncomingValues() const
LLVM Basic Block Representation.
Definition: BasicBlock.h:72
const DebugLoc & getDebugLoc() const
getDebugLoc - Return the debug location for this node as a DebugLoc.
Definition: Instruction.h:196
op_iterator op_end()
Definition: User.h:122
BasicBlock * getIncomingBlock(unsigned i) const
Value * getOperand(unsigned i) const
Definition: User.h:90
bool hasAllConstantIndices() const
static GetElementPtrInst * Create(Value *Ptr, ArrayRef< Value * > IdxList, const Twine &NameStr="", Instruction *InsertBefore=nullptr)
Definition: Instructions.h:792
Value * getIncomingValue(unsigned i) const
Type * getType() const
Definition: Value.h:215
SequentialType * getType() const
Definition: Instructions.h:830
static PHINode * Create(Type *Ty, unsigned NumReservedValues, const Twine &NameStr="", Instruction *InsertBefore=nullptr)
LLVM_ATTRIBUTE_UNUSED_RESULT std::enable_if< !is_simple_type< Y >::value, typename cast_retty< X, const Y >::ret_type >::type dyn_cast(const Y &Val)
Definition: Casting.h:265
bool hasOneUse() const
Definition: Value.h:284
Instruction * InsertNewInstBefore(Instruction *New, Instruction &Old)
Definition: InstCombine.h:259
LLVM Value Representation.
Definition: Value.h:69
Instruction * InstCombiner::FoldPHIArgLoadIntoPHI ( PHINode PN)
private

Definition at line 287 of file InstCombinePHI.cpp.

287  {
288  LoadInst *FirstLI = cast<LoadInst>(PN.getIncomingValue(0));
289 
290  // FIXME: This is overconservative; this transform is allowed in some cases
291  // for atomic operations.
292  if (FirstLI->isAtomic())
293  return nullptr;
294 
295  // When processing loads, we need to propagate two bits of information to the
296  // sunk load: whether it is volatile, and what its alignment is. We currently
297  // don't sink loads when some have their alignment specified and some don't.
298  // visitLoadInst will propagate an alignment onto the load when TD is around,
299  // and if TD isn't around, we can't handle the mixed case.
300  bool isVolatile = FirstLI->isVolatile();
301  unsigned LoadAlignment = FirstLI->getAlignment();
302  unsigned LoadAddrSpace = FirstLI->getPointerAddressSpace();
303 
304  // We can't sink the load if the loaded value could be modified between the
305  // load and the PHI.
306  if (FirstLI->getParent() != PN.getIncomingBlock(0) ||
308  return nullptr;
309 
310  // If the PHI is of volatile loads and the load block has multiple
311  // successors, sinking it would remove a load of the volatile value from
312  // the path through the other successor.
313  if (isVolatile &&
314  FirstLI->getParent()->getTerminator()->getNumSuccessors() != 1)
315  return nullptr;
316 
317  // Check to see if all arguments are the same operation.
318  for (unsigned i = 1, e = PN.getNumIncomingValues(); i != e; ++i) {
320  if (!LI || !LI->hasOneUse())
321  return nullptr;
322 
323  // We can't sink the load if the loaded value could be modified between
324  // the load and the PHI.
325  if (LI->isVolatile() != isVolatile ||
326  LI->getParent() != PN.getIncomingBlock(i) ||
327  LI->getPointerAddressSpace() != LoadAddrSpace ||
329  return nullptr;
330 
331  // If some of the loads have an alignment specified but not all of them,
332  // we can't do the transformation.
333  if ((LoadAlignment != 0) != (LI->getAlignment() != 0))
334  return nullptr;
335 
336  LoadAlignment = std::min(LoadAlignment, LI->getAlignment());
337 
338  // If the PHI is of volatile loads and the load block has multiple
339  // successors, sinking it would remove a load of the volatile value from
340  // the path through the other successor.
341  if (isVolatile &&
342  LI->getParent()->getTerminator()->getNumSuccessors() != 1)
343  return nullptr;
344  }
345 
346  // Okay, they are all the same operation. Create a new PHI node of the
347  // correct type, and PHI together all of the LHS's of the instructions.
348  PHINode *NewPN = PHINode::Create(FirstLI->getOperand(0)->getType(),
350  PN.getName()+".in");
351 
352  Value *InVal = FirstLI->getOperand(0);
353  NewPN->addIncoming(InVal, PN.getIncomingBlock(0));
354 
355  // Add all operands to the new PHI.
356  for (unsigned i = 1, e = PN.getNumIncomingValues(); i != e; ++i) {
357  Value *NewInVal = cast<LoadInst>(PN.getIncomingValue(i))->getOperand(0);
358  if (NewInVal != InVal)
359  InVal = nullptr;
360  NewPN->addIncoming(NewInVal, PN.getIncomingBlock(i));
361  }
362 
363  Value *PhiVal;
364  if (InVal) {
365  // The new PHI unions all of the same values together. This is really
366  // common, so we handle it intelligently here for compile-time speed.
367  PhiVal = InVal;
368  delete NewPN;
369  } else {
370  InsertNewInstBefore(NewPN, PN);
371  PhiVal = NewPN;
372  }
373 
374  // If this was a volatile load that we are merging, make sure to loop through
375  // and mark all the input loads as non-volatile. If we don't do this, we will
376  // insert a new volatile load and the old ones will not be deletable.
377  if (isVolatile)
378  for (unsigned i = 0, e = PN.getNumIncomingValues(); i != e; ++i)
379  cast<LoadInst>(PN.getIncomingValue(i))->setVolatile(false);
380 
381  LoadInst *NewLI = new LoadInst(PhiVal, "", isVolatile, LoadAlignment);
382  NewLI->setDebugLoc(FirstLI->getDebugLoc());
383  return NewLI;
384 }
void addIncoming(Value *V, BasicBlock *BB)
void setDebugLoc(const DebugLoc &Loc)
setDebugLoc - Set the debug location information for this instruction.
Definition: Instruction.h:193
LoopInfoBase< BlockT, LoopT > * LI
Definition: LoopInfoImpl.h:412
static bool isSafeAndProfitableToSinkLoad(LoadInst *L)
StringRef getName() const
Definition: Value.cpp:168
unsigned getNumIncomingValues() const
unsigned getNumSuccessors() const
Definition: InstrTypes.h:59
const DebugLoc & getDebugLoc() const
getDebugLoc - Return the debug location for this node as a DebugLoc.
Definition: Instruction.h:196
BasicBlock * getIncomingBlock(unsigned i) const
Value * getOperand(unsigned i) const
Definition: User.h:90
bool isAtomic() const
Definition: Instructions.h:228
Value * getIncomingValue(unsigned i) const
Type * getType() const
Definition: Value.h:215
bool isVolatile() const
Definition: Instructions.h:187
static PHINode * Create(Type *Ty, unsigned NumReservedValues, const Twine &NameStr="", Instruction *InsertBefore=nullptr)
unsigned getPointerAddressSpace() const
Returns the address space of the pointer operand.
Definition: Instructions.h:245
LLVM_ATTRIBUTE_UNUSED_RESULT std::enable_if< !is_simple_type< Y >::value, typename cast_retty< X, const Y >::ret_type >::type dyn_cast(const Y &Val)
Definition: Casting.h:265
unsigned getAlignment() const
Definition: Instructions.h:198
TerminatorInst * getTerminator()
Returns the terminator instruction if the block is well formed or null if the block is not well forme...
Definition: BasicBlock.cpp:124
bool hasOneUse() const
Definition: Value.h:284
Instruction * InsertNewInstBefore(Instruction *New, Instruction &Old)
Definition: InstCombine.h:259
LLVM Value Representation.
Definition: Value.h:69
const BasicBlock * getParent() const
Definition: Instruction.h:53
Instruction * InstCombiner::FoldPHIArgOpIntoPHI ( PHINode PN)
private

FoldPHIArgOpIntoPHI - If all operands to a PHI node are the same "unary" operator and they all are only used by the PHI, PHI together their inputs, and do the operation once, to the result of the PHI.

Definition at line 391 of file InstCombinePHI.cpp.

391  {
392  Instruction *FirstInst = cast<Instruction>(PN.getIncomingValue(0));
393 
394  if (isa<GetElementPtrInst>(FirstInst))
395  return FoldPHIArgGEPIntoPHI(PN);
396  if (isa<LoadInst>(FirstInst))
397  return FoldPHIArgLoadIntoPHI(PN);
398 
399  // Scan the instruction, looking for input operations that can be folded away.
400  // If all input operands to the phi are the same instruction (e.g. a cast from
401  // the same type or "+42") we can pull the operation through the PHI, reducing
402  // code size and simplifying code.
403  Constant *ConstantOp = nullptr;
404  Type *CastSrcTy = nullptr;
405  bool isNUW = false, isNSW = false, isExact = false;
406 
407  if (isa<CastInst>(FirstInst)) {
408  CastSrcTy = FirstInst->getOperand(0)->getType();
409 
410  // Be careful about transforming integer PHIs. We don't want to pessimize
411  // the code by turning an i32 into an i1293.
412  if (PN.getType()->isIntegerTy() && CastSrcTy->isIntegerTy()) {
413  if (!ShouldChangeType(PN.getType(), CastSrcTy))
414  return nullptr;
415  }
416  } else if (isa<BinaryOperator>(FirstInst) || isa<CmpInst>(FirstInst)) {
417  // Can fold binop, compare or shift here if the RHS is a constant,
418  // otherwise call FoldPHIArgBinOpIntoPHI.
419  ConstantOp = dyn_cast<Constant>(FirstInst->getOperand(1));
420  if (!ConstantOp)
421  return FoldPHIArgBinOpIntoPHI(PN);
422 
423  if (OverflowingBinaryOperator *BO =
424  dyn_cast<OverflowingBinaryOperator>(FirstInst)) {
425  isNUW = BO->hasNoUnsignedWrap();
426  isNSW = BO->hasNoSignedWrap();
427  } else if (PossiblyExactOperator *PEO =
428  dyn_cast<PossiblyExactOperator>(FirstInst))
429  isExact = PEO->isExact();
430  } else {
431  return nullptr; // Cannot fold this operation.
432  }
433 
434  // Check to see if all arguments are the same operation.
435  for (unsigned i = 1, e = PN.getNumIncomingValues(); i != e; ++i) {
437  if (!I || !I->hasOneUse() || !I->isSameOperationAs(FirstInst))
438  return nullptr;
439  if (CastSrcTy) {
440  if (I->getOperand(0)->getType() != CastSrcTy)
441  return nullptr; // Cast operation must match.
442  } else if (I->getOperand(1) != ConstantOp) {
443  return nullptr;
444  }
445 
446  if (isNUW)
447  isNUW = cast<OverflowingBinaryOperator>(I)->hasNoUnsignedWrap();
448  if (isNSW)
449  isNSW = cast<OverflowingBinaryOperator>(I)->hasNoSignedWrap();
450  if (isExact)
451  isExact = cast<PossiblyExactOperator>(I)->isExact();
452  }
453 
454  // Okay, they are all the same operation. Create a new PHI node of the
455  // correct type, and PHI together all of the LHS's of the instructions.
456  PHINode *NewPN = PHINode::Create(FirstInst->getOperand(0)->getType(),
458  PN.getName()+".in");
459 
460  Value *InVal = FirstInst->getOperand(0);
461  NewPN->addIncoming(InVal, PN.getIncomingBlock(0));
462 
463  // Add all operands to the new PHI.
464  for (unsigned i = 1, e = PN.getNumIncomingValues(); i != e; ++i) {
465  Value *NewInVal = cast<Instruction>(PN.getIncomingValue(i))->getOperand(0);
466  if (NewInVal != InVal)
467  InVal = nullptr;
468  NewPN->addIncoming(NewInVal, PN.getIncomingBlock(i));
469  }
470 
471  Value *PhiVal;
472  if (InVal) {
473  // The new PHI unions all of the same values together. This is really
474  // common, so we handle it intelligently here for compile-time speed.
475  PhiVal = InVal;
476  delete NewPN;
477  } else {
478  InsertNewInstBefore(NewPN, PN);
479  PhiVal = NewPN;
480  }
481 
482  // Insert and return the new operation.
483  if (CastInst *FirstCI = dyn_cast<CastInst>(FirstInst)) {
484  CastInst *NewCI = CastInst::Create(FirstCI->getOpcode(), PhiVal,
485  PN.getType());
486  NewCI->setDebugLoc(FirstInst->getDebugLoc());
487  return NewCI;
488  }
489 
490  if (BinaryOperator *BinOp = dyn_cast<BinaryOperator>(FirstInst)) {
491  BinOp = BinaryOperator::Create(BinOp->getOpcode(), PhiVal, ConstantOp);
492  if (isNUW) BinOp->setHasNoUnsignedWrap();
493  if (isNSW) BinOp->setHasNoSignedWrap();
494  if (isExact) BinOp->setIsExact();
495  BinOp->setDebugLoc(FirstInst->getDebugLoc());
496  return BinOp;
497  }
498 
499  CmpInst *CIOp = cast<CmpInst>(FirstInst);
500  CmpInst *NewCI = CmpInst::Create(CIOp->getOpcode(), CIOp->getPredicate(),
501  PhiVal, ConstantOp);
502  NewCI->setDebugLoc(FirstInst->getDebugLoc());
503  return NewCI;
504 }
Abstract base class of comparison instructions.
Definition: InstrTypes.h:654
void addIncoming(Value *V, BasicBlock *BB)
void setDebugLoc(const DebugLoc &Loc)
setDebugLoc - Set the debug location information for this instruction.
Definition: Instruction.h:193
StringRef getName() const
Definition: Value.cpp:168
Base class of casting instructions.
Definition: InstrTypes.h:387
void setHasNoUnsignedWrap(bool b=true)
Instruction * FoldPHIArgBinOpIntoPHI(PHINode &PN)
unsigned getNumIncomingValues() const
static CmpInst * Create(OtherOps Op, unsigned short predicate, Value *S1, Value *S2, const Twine &Name="", Instruction *InsertBefore=nullptr)
Create a CmpInst.
Instruction * FoldPHIArgLoadIntoPHI(PHINode &PN)
LLVM Constant Representation.
Definition: Constant.h:41
const DebugLoc & getDebugLoc() const
getDebugLoc - Return the debug location for this node as a DebugLoc.
Definition: Instruction.h:196
BasicBlock * getIncomingBlock(unsigned i) const
Value * getOperand(unsigned i) const
Definition: User.h:90
Predicate getPredicate() const
Return the predicate for this instruction.
Definition: InstrTypes.h:735
Instruction * FoldPHIArgGEPIntoPHI(PHINode &PN)
Value * getIncomingValue(unsigned i) const
Type * getType() const
Definition: Value.h:215
static PHINode * Create(Type *Ty, unsigned NumReservedValues, const Twine &NameStr="", Instruction *InsertBefore=nullptr)
bool isIntegerTy() const
Definition: Type.h:193
static BinaryOperator * Create(BinaryOps Op, Value *S1, Value *S2, const Twine &Name=Twine(), Instruction *InsertBefore=nullptr)
LLVM_ATTRIBUTE_UNUSED_RESULT std::enable_if< !is_simple_type< Y >::value, typename cast_retty< X, const Y >::ret_type >::type dyn_cast(const Y &Val)
Definition: Casting.h:265
static CastInst * Create(Instruction::CastOps, Value *S, Type *Ty, const Twine &Name="", Instruction *InsertBefore=nullptr)
Construct any of the CastInst subclasses.
bool ShouldChangeType(Type *From, Type *To) const
#define I(x, y, z)
Definition: MD5.cpp:54
bool hasOneUse() const
Definition: Value.h:284
Instruction * InsertNewInstBefore(Instruction *New, Instruction &Old)
Definition: InstCombine.h:259
OtherOps getOpcode() const
Get the opcode casted to the right type.
Definition: InstrTypes.h:730
LLVM Value Representation.
Definition: Value.h:69
bool isSameOperationAs(const Instruction *I, unsigned flags=0) const
Determine if one instruction is the same operation as another.
Instruction * InstCombiner::FoldSelectIntoOp ( SelectInst SI,
Value TrueVal,
Value FalseVal 
)

FoldSelectIntoOp - Try fold the select into one of the operands to facilitate further optimization.

Definition at line 235 of file InstCombineSelect.cpp.

236  {
237  // See the comment above GetSelectFoldableOperands for a description of the
238  // transformation we are doing here.
239  if (Instruction *TVI = dyn_cast<Instruction>(TrueVal)) {
240  if (TVI->hasOneUse() && TVI->getNumOperands() == 2 &&
241  !isa<Constant>(FalseVal)) {
242  if (unsigned SFO = GetSelectFoldableOperands(TVI)) {
243  unsigned OpToFold = 0;
244  if ((SFO & 1) && FalseVal == TVI->getOperand(0)) {
245  OpToFold = 1;
246  } else if ((SFO & 2) && FalseVal == TVI->getOperand(1)) {
247  OpToFold = 2;
248  }
249 
250  if (OpToFold) {
252  Value *OOp = TVI->getOperand(2-OpToFold);
253  // Avoid creating select between 2 constants unless it's selecting
254  // between 0, 1 and -1.
255  if (!isa<Constant>(OOp) || isSelect01(C, cast<Constant>(OOp))) {
256  Value *NewSel = Builder->CreateSelect(SI.getCondition(), OOp, C);
257  NewSel->takeName(TVI);
258  BinaryOperator *TVI_BO = cast<BinaryOperator>(TVI);
260  FalseVal, NewSel);
261  if (isa<PossiblyExactOperator>(BO))
262  BO->setIsExact(TVI_BO->isExact());
263  if (isa<OverflowingBinaryOperator>(BO)) {
264  BO->setHasNoUnsignedWrap(TVI_BO->hasNoUnsignedWrap());
265  BO->setHasNoSignedWrap(TVI_BO->hasNoSignedWrap());
266  }
267  return BO;
268  }
269  }
270  }
271  }
272  }
273 
274  if (Instruction *FVI = dyn_cast<Instruction>(FalseVal)) {
275  if (FVI->hasOneUse() && FVI->getNumOperands() == 2 &&
276  !isa<Constant>(TrueVal)) {
277  if (unsigned SFO = GetSelectFoldableOperands(FVI)) {
278  unsigned OpToFold = 0;
279  if ((SFO & 1) && TrueVal == FVI->getOperand(0)) {
280  OpToFold = 1;
281  } else if ((SFO & 2) && TrueVal == FVI->getOperand(1)) {
282  OpToFold = 2;
283  }
284 
285  if (OpToFold) {
287  Value *OOp = FVI->getOperand(2-OpToFold);
288  // Avoid creating select between 2 constants unless it's selecting
289  // between 0, 1 and -1.
290  if (!isa<Constant>(OOp) || isSelect01(C, cast<Constant>(OOp))) {
291  Value *NewSel = Builder->CreateSelect(SI.getCondition(), C, OOp);
292  NewSel->takeName(FVI);
293  BinaryOperator *FVI_BO = cast<BinaryOperator>(FVI);
295  TrueVal, NewSel);
296  if (isa<PossiblyExactOperator>(BO))
297  BO->setIsExact(FVI_BO->isExact());
298  if (isa<OverflowingBinaryOperator>(BO)) {
299  BO->setHasNoUnsignedWrap(FVI_BO->hasNoUnsignedWrap());
300  BO->setHasNoSignedWrap(FVI_BO->hasNoSignedWrap());
301  }
302  return BO;
303  }
304  }
305  }
306  }
307  }
308 
309  return nullptr;
310 }
static Constant * GetSelectFoldableConstant(Instruction *I)
void takeName(Value *V)
Definition: Value.cpp:242
LLVM Constant Representation.
Definition: Constant.h:41
const Value * getCondition() const
BuilderTy * Builder
Definition: InstCombine.h:102
bool hasNoSignedWrap() const
hasNoSignedWrap - Determine whether the no signed wrap flag is set.
void setIsExact(bool b=true)
BinaryOps getOpcode() const
Definition: InstrTypes.h:326
bool isExact() const
isExact - Determine whether the exact flag is set.
static BinaryOperator * Create(BinaryOps Op, Value *S1, Value *S2, const Twine &Name=Twine(), Instruction *InsertBefore=nullptr)
Value * CreateSelect(Value *C, Value *True, Value *False, const Twine &Name="")
Definition: IRBuilder.h:1377
LLVM Value Representation.
Definition: Value.h:69
bool hasNoUnsignedWrap() const
hasNoUnsignedWrap - Determine whether the no unsigned wrap flag is set.
static bool isSelect01(Constant *C1, Constant *C2)
static unsigned GetSelectFoldableOperands(Instruction *I)
Instruction * InstCombiner::FoldSelectOpOp ( SelectInst SI,
Instruction TI,
Instruction FI 
)

FoldSelectOpOp - Here we have (select c, TI, FI), and we know that TI and FI have the same opcode and only one use each. Try to simplify this.

Definition at line 148 of file InstCombineSelect.cpp.

149  {
150  if (TI->getNumOperands() == 1) {
151  // If this is a non-volatile load or a cast from the same type,
152  // merge.
153  if (TI->isCast()) {
154  Type *FIOpndTy = FI->getOperand(0)->getType();
155  if (TI->getOperand(0)->getType() != FIOpndTy)
156  return nullptr;
157  // The select condition may be a vector. We may only change the operand
158  // type if the vector width remains the same (and matches the condition).
159  Type *CondTy = SI.getCondition()->getType();
160  if (CondTy->isVectorTy() && (!FIOpndTy->isVectorTy() ||
161  CondTy->getVectorNumElements() != FIOpndTy->getVectorNumElements()))
162  return nullptr;
163  } else {
164  return nullptr; // unknown unary op.
165  }
166 
167  // Fold this by inserting a select from the input values.
168  Value *NewSI = Builder->CreateSelect(SI.getCondition(), TI->getOperand(0),
169  FI->getOperand(0), SI.getName()+".v");
170  return CastInst::Create(Instruction::CastOps(TI->getOpcode()), NewSI,
171  TI->getType());
172  }
173 
174  // Only handle binary operators here.
175  if (!isa<BinaryOperator>(TI))
176  return nullptr;
177 
178  // Figure out if the operations have any operands in common.
179  Value *MatchOp, *OtherOpT, *OtherOpF;
180  bool MatchIsOpZero;
181  if (TI->getOperand(0) == FI->getOperand(0)) {
182  MatchOp = TI->getOperand(0);
183  OtherOpT = TI->getOperand(1);
184  OtherOpF = FI->getOperand(1);
185  MatchIsOpZero = true;
186  } else if (TI->getOperand(1) == FI->getOperand(1)) {
187  MatchOp = TI->getOperand(1);
188  OtherOpT = TI->getOperand(0);
189  OtherOpF = FI->getOperand(0);
190  MatchIsOpZero = false;
191  } else if (!TI->isCommutative()) {
192  return nullptr;
193  } else if (TI->getOperand(0) == FI->getOperand(1)) {
194  MatchOp = TI->getOperand(0);
195  OtherOpT = TI->getOperand(1);
196  OtherOpF = FI->getOperand(0);
197  MatchIsOpZero = true;
198  } else if (TI->getOperand(1) == FI->getOperand(0)) {
199  MatchOp = TI->getOperand(1);
200  OtherOpT = TI->getOperand(0);
201  OtherOpF = FI->getOperand(1);
202  MatchIsOpZero = true;
203  } else {
204  return nullptr;
205  }
206 
207  // If we reach here, they do have operations in common.
208  Value *NewSI = Builder->CreateSelect(SI.getCondition(), OtherOpT,
209  OtherOpF, SI.getName()+".v");
210 
211  if (BinaryOperator *BO = dyn_cast<BinaryOperator>(TI)) {
212  if (MatchIsOpZero)
213  return BinaryOperator::Create(BO->getOpcode(), MatchOp, NewSI);
214  else
215  return BinaryOperator::Create(BO->getOpcode(), NewSI, MatchOp);
216  }
217  llvm_unreachable("Shouldn't get here");
218 }
unsigned getNumOperands() const
Definition: User.h:110
StringRef getName() const
Definition: Value.cpp:168
bool isCast() const
Definition: Instruction.h:92
#define llvm_unreachable(msg)
Definition: ErrorHandling.h:98
bool isVectorTy() const
Definition: Type.h:226
const Value * getCondition() const
Value * getOperand(unsigned i) const
Definition: User.h:90
bool isCommutative() const
Definition: Instruction.h:287
BuilderTy * Builder
Definition: InstCombine.h:102
unsigned getVectorNumElements() const
Definition: Type.cpp:208
Type * getType() const
Definition: Value.h:215
static BinaryOperator * Create(BinaryOps Op, Value *S1, Value *S2, const Twine &Name=Twine(), Instruction *InsertBefore=nullptr)
Value * CreateSelect(Value *C, Value *True, Value *False, const Twine &Name="")
Definition: IRBuilder.h:1377
static CastInst * Create(Instruction::CastOps, Value *S, Type *Ty, const Twine &Name="", Instruction *InsertBefore=nullptr)
Construct any of the CastInst subclasses.
LLVM Value Representation.
Definition: Value.h:69
unsigned getOpcode() const
getOpcode() returns a member of one of the enums like Instruction::Add.
Definition: Instruction.h:86
Instruction * InstCombiner::FoldShiftByConstant ( Value Op0,
Constant Op1,
BinaryOperator I 
)

Definition at line 314 of file InstCombineShifts.cpp.

315  {
316  bool isLeftShift = I.getOpcode() == Instruction::Shl;
317 
318  ConstantInt *COp1 = nullptr;
319  if (ConstantDataVector *CV = dyn_cast<ConstantDataVector>(Op1))
320  COp1 = dyn_cast_or_null<ConstantInt>(CV->getSplatValue());
321  else if (ConstantVector *CV = dyn_cast<ConstantVector>(Op1))
322  COp1 = dyn_cast_or_null<ConstantInt>(CV->getSplatValue());
323  else
324  COp1 = dyn_cast<ConstantInt>(Op1);
325 
326  if (!COp1)
327  return nullptr;
328 
329  // See if we can propagate this shift into the input, this covers the trivial
330  // cast of lshr(shl(x,c1),c2) as well as other more complex cases.
331  if (I.getOpcode() != Instruction::AShr &&
332  CanEvaluateShifted(Op0, COp1->getZExtValue(), isLeftShift, *this)) {
333  DEBUG(dbgs() << "ICE: GetShiftedValue propagating shift through expression"
334  " to eliminate shift:\n IN: " << *Op0 << "\n SH: " << I <<"\n");
335 
336  return ReplaceInstUsesWith(I,
337  GetShiftedValue(Op0, COp1->getZExtValue(), isLeftShift, *this));
338  }
339 
340  // See if we can simplify any instructions used by the instruction whose sole
341  // purpose is to compute bits we don't care about.
342  uint32_t TypeBits = Op0->getType()->getScalarSizeInBits();
343 
344  assert(!COp1->uge(TypeBits) &&
345  "Shift over the type width should have been removed already");
346 
347  // ((X*C1) << C2) == (X * (C1 << C2))
348  if (BinaryOperator *BO = dyn_cast<BinaryOperator>(Op0))
349  if (BO->getOpcode() == Instruction::Mul && isLeftShift)
350  if (Constant *BOOp = dyn_cast<Constant>(BO->getOperand(1)))
351  return BinaryOperator::CreateMul(BO->getOperand(0),
352  ConstantExpr::getShl(BOOp, Op1));
353 
354  // Try to fold constant and into select arguments.
355  if (SelectInst *SI = dyn_cast<SelectInst>(Op0))
356  if (Instruction *R = FoldOpIntoSelect(I, SI))
357  return R;
358  if (isa<PHINode>(Op0))
359  if (Instruction *NV = FoldOpIntoPhi(I))
360  return NV;
361 
362  // Fold shift2(trunc(shift1(x,c1)), c2) -> trunc(shift2(shift1(x,c1),c2))
363  if (TruncInst *TI = dyn_cast<TruncInst>(Op0)) {
364  Instruction *TrOp = dyn_cast<Instruction>(TI->getOperand(0));
365  // If 'shift2' is an ashr, we would have to get the sign bit into a funny
366  // place. Don't try to do this transformation in this case. Also, we
367  // require that the input operand is a shift-by-constant so that we have
368  // confidence that the shifts will get folded together. We could do this
369  // xform in more cases, but it is unlikely to be profitable.
370  if (TrOp && I.isLogicalShift() && TrOp->isShift() &&
371  isa<ConstantInt>(TrOp->getOperand(1))) {
372  // Okay, we'll do this xform. Make the shift of shift.
373  Constant *ShAmt = ConstantExpr::getZExt(COp1, TrOp->getType());
374  // (shift2 (shift1 & 0x00FF), c2)
375  Value *NSh = Builder->CreateBinOp(I.getOpcode(), TrOp, ShAmt,I.getName());
376 
377  // For logical shifts, the truncation has the effect of making the high
378  // part of the register be zeros. Emulate this by inserting an AND to
379  // clear the top bits as needed. This 'and' will usually be zapped by
380  // other xforms later if dead.
381  unsigned SrcSize = TrOp->getType()->getScalarSizeInBits();
382  unsigned DstSize = TI->getType()->getScalarSizeInBits();
383  APInt MaskV(APInt::getLowBitsSet(SrcSize, DstSize));
384 
385  // The mask we constructed says what the trunc would do if occurring
386  // between the shifts. We want to know the effect *after* the second
387  // shift. We know that it is a logical shift by a constant, so adjust the
388  // mask as appropriate.
389  if (I.getOpcode() == Instruction::Shl)
390  MaskV <<= COp1->getZExtValue();
391  else {
392  assert(I.getOpcode() == Instruction::LShr && "Unknown logical shift");
393  MaskV = MaskV.lshr(COp1->getZExtValue());
394  }
395 
396  // shift1 & 0x00FF
397  Value *And = Builder->CreateAnd(NSh,
398  ConstantInt::get(I.getContext(), MaskV),
399  TI->getName());
400 
401  // Return the value truncated to the interesting size.
402  return new TruncInst(And, I.getType());
403  }
404  }
405 
406  if (Op0->hasOneUse()) {
407  if (BinaryOperator *Op0BO = dyn_cast<BinaryOperator>(Op0)) {
408  // Turn ((X >> C) + Y) << C -> (X + (Y << C)) & (~0 << C)
409  Value *V1, *V2;
410  ConstantInt *CC;
411  switch (Op0BO->getOpcode()) {
412  default: break;
413  case Instruction::Add:
414  case Instruction::And:
415  case Instruction::Or:
416  case Instruction::Xor: {
417  // These operators commute.
418  // Turn (Y + (X >> C)) << C -> (X + (Y << C)) & (~0 << C)
419  if (isLeftShift && Op0BO->getOperand(1)->hasOneUse() &&
420  match(Op0BO->getOperand(1), m_Shr(m_Value(V1),
421  m_Specific(Op1)))) {
422  Value *YS = // (Y << C)
423  Builder->CreateShl(Op0BO->getOperand(0), Op1, Op0BO->getName());
424  // (X + (Y << C))
425  Value *X = Builder->CreateBinOp(Op0BO->getOpcode(), YS, V1,
426  Op0BO->getOperand(1)->getName());
427  uint32_t Op1Val = COp1->getLimitedValue(TypeBits);
428 
429  APInt Bits = APInt::getHighBitsSet(TypeBits, TypeBits - Op1Val);
431  if (VectorType *VT = dyn_cast<VectorType>(X->getType()))
432  Mask = ConstantVector::getSplat(VT->getNumElements(), Mask);
433  return BinaryOperator::CreateAnd(X, Mask);
434  }
435 
436  // Turn (Y + ((X >> C) & CC)) << C -> ((X & (CC << C)) + (Y << C))
437  Value *Op0BOOp1 = Op0BO->getOperand(1);
438  if (isLeftShift && Op0BOOp1->hasOneUse() &&
439  match(Op0BOOp1,
440  m_And(m_OneUse(m_Shr(m_Value(V1), m_Specific(Op1))),
441  m_ConstantInt(CC)))) {
442  Value *YS = // (Y << C)
443  Builder->CreateShl(Op0BO->getOperand(0), Op1,
444  Op0BO->getName());
445  // X & (CC << C)
446  Value *XM = Builder->CreateAnd(V1, ConstantExpr::getShl(CC, Op1),
447  V1->getName()+".mask");
448  return BinaryOperator::Create(Op0BO->getOpcode(), YS, XM);
449  }
450  }
451 
452  // FALL THROUGH.
453  case Instruction::Sub: {
454  // Turn ((X >> C) + Y) << C -> (X + (Y << C)) & (~0 << C)
455  if (isLeftShift && Op0BO->getOperand(0)->hasOneUse() &&
456  match(Op0BO->getOperand(0), m_Shr(m_Value(V1),
457  m_Specific(Op1)))) {
458  Value *YS = // (Y << C)
459  Builder->CreateShl(Op0BO->getOperand(1), Op1, Op0BO->getName());
460  // (X + (Y << C))
461  Value *X = Builder->CreateBinOp(Op0BO->getOpcode(), V1, YS,
462  Op0BO->getOperand(0)->getName());
463  uint32_t Op1Val = COp1->getLimitedValue(TypeBits);
464 
465  APInt Bits = APInt::getHighBitsSet(TypeBits, TypeBits - Op1Val);
467  if (VectorType *VT = dyn_cast<VectorType>(X->getType()))
468  Mask = ConstantVector::getSplat(VT->getNumElements(), Mask);
469  return BinaryOperator::CreateAnd(X, Mask);
470  }
471 
472  // Turn (((X >> C)&CC) + Y) << C -> (X + (Y << C)) & (CC << C)
473  if (isLeftShift && Op0BO->getOperand(0)->hasOneUse() &&
474  match(Op0BO->getOperand(0),
475  m_And(m_OneUse(m_Shr(m_Value(V1), m_Value(V2))),
476  m_ConstantInt(CC))) && V2 == Op1) {
477  Value *YS = // (Y << C)
478  Builder->CreateShl(Op0BO->getOperand(1), Op1, Op0BO->getName());
479  // X & (CC << C)
480  Value *XM = Builder->CreateAnd(V1, ConstantExpr::getShl(CC, Op1),
481  V1->getName()+".mask");
482 
483  return BinaryOperator::Create(Op0BO->getOpcode(), XM, YS);
484  }
485 
486  break;
487  }
488  }
489 
490 
491  // If the operand is an bitwise operator with a constant RHS, and the
492  // shift is the only use, we can pull it out of the shift.
493  if (ConstantInt *Op0C = dyn_cast<ConstantInt>(Op0BO->getOperand(1))) {
494  bool isValid = true; // Valid only for And, Or, Xor
495  bool highBitSet = false; // Transform if high bit of constant set?
496 
497  switch (Op0BO->getOpcode()) {
498  default: isValid = false; break; // Do not perform transform!
499  case Instruction::Add:
500  isValid = isLeftShift;
501  break;
502  case Instruction::Or:
503  case Instruction::Xor:
504  highBitSet = false;
505  break;
506  case Instruction::And:
507  highBitSet = true;
508  break;
509  }
510 
511  // If this is a signed shift right, and the high bit is modified
512  // by the logical operation, do not perform the transformation.
513  // The highBitSet boolean indicates the value of the high bit of
514  // the constant which would cause it to be modified for this
515  // operation.
516  //
517  if (isValid && I.getOpcode() == Instruction::AShr)
518  isValid = Op0C->getValue()[TypeBits-1] == highBitSet;
519 
520  if (isValid) {
521  Constant *NewRHS = ConstantExpr::get(I.getOpcode(), Op0C, Op1);
522 
523  Value *NewShift =
524  Builder->CreateBinOp(I.getOpcode(), Op0BO->getOperand(0), Op1);
525  NewShift->takeName(Op0BO);
526 
527  return BinaryOperator::Create(Op0BO->getOpcode(), NewShift,
528  NewRHS);
529  }
530  }
531  }
532  }
533 
534  // Find out if this is a shift of a shift by a constant.
535  BinaryOperator *ShiftOp = dyn_cast<BinaryOperator>(Op0);
536  if (ShiftOp && !ShiftOp->isShift())
537  ShiftOp = nullptr;
538 
539  if (ShiftOp && isa<ConstantInt>(ShiftOp->getOperand(1))) {
540 
541  // This is a constant shift of a constant shift. Be careful about hiding
542  // shl instructions behind bit masks. They are used to represent multiplies
543  // by a constant, and it is important that simple arithmetic expressions
544  // are still recognizable by scalar evolution.
545  //
546  // The transforms applied to shl are very similar to the transforms applied
547  // to mul by constant. We can be more aggressive about optimizing right
548  // shifts.
549  //
550  // Combinations of right and left shifts will still be optimized in
551  // DAGCombine where scalar evolution no longer applies.
552 
553  ConstantInt *ShiftAmt1C = cast<ConstantInt>(ShiftOp->getOperand(1));
554  uint32_t ShiftAmt1 = ShiftAmt1C->getLimitedValue(TypeBits);
555  uint32_t ShiftAmt2 = COp1->getLimitedValue(TypeBits);
556  assert(ShiftAmt2 != 0 && "Should have been simplified earlier");
557  if (ShiftAmt1 == 0) return nullptr; // Will be simplified in the future.
558  Value *X = ShiftOp->getOperand(0);
559 
560  IntegerType *Ty = cast<IntegerType>(I.getType());
561 
562  // Check for (X << c1) << c2 and (X >> c1) >> c2
563  if (I.getOpcode() == ShiftOp->getOpcode()) {
564  uint32_t AmtSum = ShiftAmt1+ShiftAmt2; // Fold into one big shift.
565  // If this is oversized composite shift, then unsigned shifts get 0, ashr
566  // saturates.
567  if (AmtSum >= TypeBits) {
568  if (I.getOpcode() != Instruction::AShr)
570  AmtSum = TypeBits-1; // Saturate to 31 for i32 ashr.
571  }
572 
573  return BinaryOperator::Create(I.getOpcode(), X,
574  ConstantInt::get(Ty, AmtSum));
575  }
576 
577  if (ShiftAmt1 == ShiftAmt2) {
578  // If we have ((X << C) >>u C), turn this into X & (-1 >>u C).
579  if (I.getOpcode() == Instruction::LShr &&
580  ShiftOp->getOpcode() == Instruction::Shl) {
581  APInt Mask(APInt::getLowBitsSet(TypeBits, TypeBits - ShiftAmt1));
582  return BinaryOperator::CreateAnd(X,
583  ConstantInt::get(I.getContext(), Mask));
584  }
585  } else if (ShiftAmt1 < ShiftAmt2) {
586  uint32_t ShiftDiff = ShiftAmt2-ShiftAmt1;
587 
588  // (X >>?,exact C1) << C2 --> X << (C2-C1)
589  // The inexact version is deferred to DAGCombine so we don't hide shl
590  // behind a bit mask.
591  if (I.getOpcode() == Instruction::Shl &&
592  ShiftOp->getOpcode() != Instruction::Shl &&
593  ShiftOp->isExact()) {
594  assert(ShiftOp->getOpcode() == Instruction::LShr ||
595  ShiftOp->getOpcode() == Instruction::AShr);
596  ConstantInt *ShiftDiffCst = ConstantInt::get(Ty, ShiftDiff);
597  BinaryOperator *NewShl = BinaryOperator::Create(Instruction::Shl,
598  X, ShiftDiffCst);
600  NewShl->setHasNoSignedWrap(I.hasNoSignedWrap());
601  return NewShl;
602  }
603 
604  // (X << C1) >>u C2 --> X >>u (C2-C1) & (-1 >> C2)
605  if (I.getOpcode() == Instruction::LShr &&
606  ShiftOp->getOpcode() == Instruction::Shl) {
607  ConstantInt *ShiftDiffCst = ConstantInt::get(Ty, ShiftDiff);
608  // (X <<nuw C1) >>u C2 --> X >>u (C2-C1)
609  if (ShiftOp->hasNoUnsignedWrap()) {
610  BinaryOperator *NewLShr = BinaryOperator::Create(Instruction::LShr,
611  X, ShiftDiffCst);
612  NewLShr->setIsExact(I.isExact());
613  return NewLShr;
614  }
615  Value *Shift = Builder->CreateLShr(X, ShiftDiffCst);
616 
617  APInt Mask(APInt::getLowBitsSet(TypeBits, TypeBits - ShiftAmt2));
618  return BinaryOperator::CreateAnd(Shift,
619  ConstantInt::get(I.getContext(),Mask));
620  }
621 
622  // We can't handle (X << C1) >>s C2, it shifts arbitrary bits in. However,
623  // we can handle (X <<nsw C1) >>s C2 since it only shifts in sign bits.
624  if (I.getOpcode() == Instruction::AShr &&
625  ShiftOp->getOpcode() == Instruction::Shl) {
626  if (ShiftOp->hasNoSignedWrap()) {
627  // (X <<nsw C1) >>s C2 --> X >>s (C2-C1)
628  ConstantInt *ShiftDiffCst = ConstantInt::get(Ty, ShiftDiff);
629  BinaryOperator *NewAShr = BinaryOperator::Create(Instruction::AShr,
630  X, ShiftDiffCst);
631  NewAShr->setIsExact(I.isExact());
632  return NewAShr;
633  }
634  }
635  } else {
636  assert(ShiftAmt2 < ShiftAmt1);
637  uint32_t ShiftDiff = ShiftAmt1-ShiftAmt2;
638 
639  // (X >>?exact C1) << C2 --> X >>?exact (C1-C2)
640  // The inexact version is deferred to DAGCombine so we don't hide shl
641  // behind a bit mask.
642  if (I.getOpcode() == Instruction::Shl &&
643  ShiftOp->getOpcode() != Instruction::Shl &&
644  ShiftOp->isExact()) {
645  ConstantInt *ShiftDiffCst = ConstantInt::get(Ty, ShiftDiff);
646  BinaryOperator *NewShr = BinaryOperator::Create(ShiftOp->getOpcode(),
647  X, ShiftDiffCst);
648  NewShr->setIsExact(true);
649  return NewShr;
650  }
651 
652  // (X << C1) >>u C2 --> X << (C1-C2) & (-1 >> C2)
653  if (I.getOpcode() == Instruction::LShr &&
654  ShiftOp->getOpcode() == Instruction::Shl) {
655  ConstantInt *ShiftDiffCst = ConstantInt::get(Ty, ShiftDiff);
656  if (ShiftOp->hasNoUnsignedWrap()) {
657  // (X <<nuw C1) >>u C2 --> X <<nuw (C1-C2)
658  BinaryOperator *NewShl = BinaryOperator::Create(Instruction::Shl,
659  X, ShiftDiffCst);
660  NewShl->setHasNoUnsignedWrap(true);
661  return NewShl;
662  }
663  Value *Shift = Builder->CreateShl(X, ShiftDiffCst);
664 
665  APInt Mask(APInt::getLowBitsSet(TypeBits, TypeBits - ShiftAmt2));
666  return BinaryOperator::CreateAnd(Shift,
667  ConstantInt::get(I.getContext(),Mask));
668  }
669 
670  // We can't handle (X << C1) >>s C2, it shifts arbitrary bits in. However,
671  // we can handle (X <<nsw C1) >>s C2 since it only shifts in sign bits.
672  if (I.getOpcode() == Instruction::AShr &&
673  ShiftOp->getOpcode() == Instruction::Shl) {
674  if (ShiftOp->hasNoSignedWrap()) {
675  // (X <<nsw C1) >>s C2 --> X <<nsw (C1-C2)
676  ConstantInt *ShiftDiffCst = ConstantInt::get(Ty, ShiftDiff);
677  BinaryOperator *NewShl = BinaryOperator::Create(Instruction::Shl,
678  X, ShiftDiffCst);
679  NewShl->setHasNoSignedWrap(true);
680  return NewShl;
681  }
682  }
683  }
684  }
685  return nullptr;
686 }
Value * CreateLShr(Value *LHS, Value *RHS, const Twine &Name="", bool isExact=false)
Definition: IRBuilder.h:770
Instruction * FoldOpIntoPhi(Instruction &I)
BinaryOp_match< LHS, RHS, Instruction::And > m_And(const LHS &L, const RHS &R)
Definition: PatternMatch.h:467
class_match< Value > m_Value()
m_Value() - Match an arbitrary value and ignore it.
Definition: PatternMatch.h:70
void setHasNoSignedWrap(bool b=true)
static APInt getLowBitsSet(unsigned numBits, unsigned loBitsSet)
Get a value with low bits set.
Definition: APInt.h:526
static RegisterPass< NVPTXAllocaHoisting > X("alloca-hoisting","Hoisting alloca instructions in non-entry ""blocks to the entry block")
static bool CanEvaluateShifted(Value *V, unsigned NumBits, bool isLeftShift, InstCombiner &IC)
Instruction * FoldOpIntoSelect(Instruction &Op, SelectInst *SI)
Value * CreateBinOp(Instruction::BinaryOps Opc, Value *LHS, Value *RHS, const Twine &Name="", MDNode *FPMathTag=nullptr)
Definition: IRBuilder.h:851
static Constant * getNullValue(Type *Ty)
Definition: Constants.cpp:133
StringRef getName() const
Definition: Value.cpp:168
bool uge(uint64_t Num) const
Determine if the value is greater or equal to the given number.
Definition: Constants.h:209
bool match(Val *V, const Pattern &P)
Definition: PatternMatch.h:42
static Constant * get(unsigned Opcode, Constant *C1, Constant *C2, unsigned Flags=0)
Definition: Constants.cpp:1752
BinOp2_match< LHS, RHS, Instruction::LShr, Instruction::AShr > m_Shr(const LHS &L, const RHS &R)
m_Shr - Matches LShr or AShr.
Definition: PatternMatch.h:618
void setHasNoUnsignedWrap(bool b=true)
uint64_t getZExtValue() const
Return the zero extended value.
Definition: Constants.h:116
Value * CreateAnd(Value *LHS, Value *RHS, const Twine &Name="")
Definition: IRBuilder.h:806
uint64_t getLimitedValue(uint64_t Limit=~0ULL) const
Get the constant's value with a saturation limit.
Definition: Constants.h:218
bool isLogicalShift() const
Definition: Instruction.h:111
class_match< ConstantInt > m_ConstantInt()
m_ConstantInt() - Match an arbitrary ConstantInt and ignore it.
Definition: PatternMatch.h:72
void takeName(Value *V)
Definition: Value.cpp:242
This class represents a truncation of integer types.
static APInt getHighBitsSet(unsigned numBits, unsigned hiBitsSet)
Get a value with high bits set.
Definition: APInt.h:508
OneUse_match< T > m_OneUse(const T &SubPattern)
Definition: PatternMatch.h:60
assert(Globals.size() > 1)
LLVM Constant Representation.
Definition: Constant.h:41
Instruction * ReplaceInstUsesWith(Instruction &I, Value *V)
Definition: InstCombine.h:282
APInt Or(const APInt &LHS, const APInt &RHS)
Bitwise OR function for APInt.
Definition: APInt.h:1870
APInt Xor(const APInt &LHS, const APInt &RHS)
Bitwise XOR function for APInt.
Definition: APInt.h:1875
specificval_ty m_Specific(const Value *V)
m_Specific - Match if we have a specific specified value.
Definition: PatternMatch.h:323
Value * getOperand(unsigned i) const
Definition: User.h:90
Integer representation type.
Definition: DerivedTypes.h:37
#define DEBUG(X)
BuilderTy * Builder
Definition: InstCombine.h:102
LLVMContext & getContext() const
All values hold a context through their type.
Definition: Value.cpp:615
bool hasNoSignedWrap() const
hasNoSignedWrap - Determine whether the no signed wrap flag is set.
void setIsExact(bool b=true)
BinaryOps getOpcode() const
Definition: InstrTypes.h:326
static Constant * getSplat(unsigned NumElts, Constant *Elt)
Definition: Constants.cpp:1059
Class for constant integers.
Definition: Constants.h:51
unsigned getScalarSizeInBits() const LLVM_READONLY
Definition: Type.cpp:135
Type * getType() const
Definition: Value.h:215
static Value * GetShiftedValue(Value *V, unsigned NumBits, bool isLeftShift, InstCombiner &IC)
static Constant * get(Type *Ty, uint64_t V, bool isSigned=false)
Definition: Constants.cpp:528
bool isExact() const
isExact - Determine whether the exact flag is set.
raw_ostream & dbgs()
dbgs - Return a circular-buffered debug stream.
Class for arbitrary precision integers.
Definition: APInt.h:75
static BinaryOperator * Create(BinaryOps Op, Value *S1, Value *S2, const Twine &Name=Twine(), Instruction *InsertBefore=nullptr)
LLVM_ATTRIBUTE_UNUSED_RESULT std::enable_if< !is_simple_type< Y >::value, typename cast_retty< X, const Y >::ret_type >::type dyn_cast(const Y &Val)
Definition: Casting.h:265
APInt And(const APInt &LHS, const APInt &RHS)
Bitwise AND function for APInt.
Definition: APInt.h:1865
Value * CreateShl(Value *LHS, Value *RHS, const Twine &Name="", bool HasNUW=false, bool HasNSW=false)
Definition: IRBuilder.h:751
static Constant * getZExt(Constant *C, Type *Ty)
Definition: Constants.cpp:1615
bool hasOneUse() const
Definition: Value.h:284
static Constant * getShl(Constant *C1, Constant *C2, bool HasNUW=false, bool HasNSW=false)
Definition: Constants.cpp:2173
LLVM Value Representation.
Definition: Value.h:69
bool hasNoUnsignedWrap() const
hasNoUnsignedWrap - Determine whether the no unsigned wrap flag is set.
Instruction * InstCombiner::FoldSPFofSPF ( Instruction Inner,
SelectPatternFlavor  SPF1,
Value A,
Value B,
Instruction Outer,
SelectPatternFlavor  SPF2,
Value C 
)

FoldSPFofSPF - We have an SPF (e.g. a min or max) of an SPF of the form: SPF2(SPF1(A, B), C)

Definition at line 683 of file InstCombineSelect.cpp.

687  {
688  if (C == A || C == B) {
689  // MAX(MAX(A, B), B) -> MAX(A, B)
690  // MIN(MIN(a, b), a) -> MIN(a, b)
691  if (SPF1 == SPF2)
692  return ReplaceInstUsesWith(Outer, Inner);
693 
694  // MAX(MIN(a, b), a) -> a
695  // MIN(MAX(a, b), a) -> a
696  if ((SPF1 == SPF_SMIN && SPF2 == SPF_SMAX) ||
697  (SPF1 == SPF_SMAX && SPF2 == SPF_SMIN) ||
698  (SPF1 == SPF_UMIN && SPF2 == SPF_UMAX) ||
699  (SPF1 == SPF_UMAX && SPF2 == SPF_UMIN))
700  return ReplaceInstUsesWith(Outer, C);
701  }
702 
703  if (SPF1 == SPF2) {
704  if (ConstantInt *CB = dyn_cast<ConstantInt>(B)) {
705  if (ConstantInt *CC = dyn_cast<ConstantInt>(C)) {
706  APInt ACB = CB->getValue();
707  APInt ACC = CC->getValue();
708 
709  // MIN(MIN(A, 23), 97) -> MIN(A, 23)
710  // MAX(MAX(A, 97), 23) -> MAX(A, 97)
711  if ((SPF1 == SPF_UMIN && ACB.ule(ACC)) ||
712  (SPF1 == SPF_SMIN && ACB.sle(ACC)) ||
713  (SPF1 == SPF_UMAX && ACB.uge(ACC)) ||
714  (SPF1 == SPF_SMAX && ACB.sge(ACC)))
715  return ReplaceInstUsesWith(Outer, Inner);
716 
717  // MIN(MIN(A, 97), 23) -> MIN(A, 23)
718  // MAX(MAX(A, 23), 97) -> MAX(A, 97)
719  if ((SPF1 == SPF_UMIN && ACB.ugt(ACC)) ||
720  (SPF1 == SPF_SMIN && ACB.sgt(ACC)) ||
721  (SPF1 == SPF_UMAX && ACB.ult(ACC)) ||
722  (SPF1 == SPF_SMAX && ACB.slt(ACC))) {
723  Outer.replaceUsesOfWith(Inner, A);
724  return &Outer;
725  }
726  }
727  }
728  }
729 
730  // ABS(ABS(X)) -> ABS(X)
731  // NABS(NABS(X)) -> NABS(X)
732  if (SPF1 == SPF2 && (SPF1 == SPF_ABS || SPF1 == SPF_NABS)) {
733  return ReplaceInstUsesWith(Outer, Inner);
734  }
735 
736  // ABS(NABS(X)) -> ABS(X)
737  // NABS(ABS(X)) -> NABS(X)
738  if ((SPF1 == SPF_ABS && SPF2 == SPF_NABS) ||
739  (SPF1 == SPF_NABS && SPF2 == SPF_ABS)) {
740  SelectInst *SI = cast<SelectInst>(Inner);
741  Value *NewSI = Builder->CreateSelect(
742  SI->getCondition(), SI->getFalseValue(), SI->getTrueValue());
743  return ReplaceInstUsesWith(Outer, NewSI);
744  }
745  return nullptr;
746 }
bool sgt(const APInt &RHS) const
Signed greather than comparison.
Definition: APInt.h:1096
bool ult(const APInt &RHS) const
Unsigned less than comparison.
Definition: APInt.cpp:516
void replaceUsesOfWith(Value *From, Value *To)
Definition: User.cpp:26
bool sge(const APInt &RHS) const
Signed greather or equal comparison.
Definition: APInt.h:1128
const Value * getCondition() const
Instruction * ReplaceInstUsesWith(Instruction &I, Value *V)
Definition: InstCombine.h:282
bool sle(const APInt &RHS) const
Signed less or equal comparison.
Definition: APInt.h:1064
bool uge(const APInt &RHS) const
Unsigned greater or equal comparison.
Definition: APInt.h:1112
BuilderTy * Builder
Definition: InstCombine.h:102
const Value * getTrueValue() const
bool ugt(const APInt &RHS) const
Unsigned greather than comparison.
Definition: APInt.h:1080
Class for constant integers.
Definition: Constants.h:51
bool slt(const APInt &RHS) const
Signed less than comparison.
Definition: APInt.cpp:548
Class for arbitrary precision integers.
Definition: APInt.h:75
Value * CreateSelect(Value *C, Value *True, Value *False, const Twine &Name="")
Definition: IRBuilder.h:1377
LLVM Value Representation.
Definition: Value.h:69
const Value * getFalseValue() const
bool ule(const APInt &RHS) const
Unsigned less or equal comparison.
Definition: APInt.h:1048
void * Pass::getAdjustedAnalysisPointer ( AnalysisID  ID)
virtualinherited

getAdjustedAnalysisPointer - This method is used when a pass implements an analysis interface through multiple inheritance. If needed, it should override this to adjust the this pointer as needed for the specified pass info.

Reimplemented in llvm::LibCallAliasAnalysis, and llvm::objcarc::ObjCARCAliasAnalysis.

Definition at line 90 of file Pass.cpp.

90  {
91  return this;
92 }
template<typename AnalysisType >
AnalysisType & llvm::Pass::getAnalysis ( ) const
inherited

getAnalysis<AnalysisType>() - This function is used by subclasses to get to the analysis information that they claim to use by overriding the getAnalysisUsage function.

Definition at line 199 of file PassAnalysisSupport.h.

199  {
200  assert(Resolver && "Pass has not been inserted into a PassManager object!");
201  return getAnalysisID<AnalysisType>(&AnalysisType::ID);
202 }
assert(Globals.size() > 1)
AnalysisResolver * Resolver
Definition: Pass.h:83
template<typename AnalysisType >
AnalysisType & llvm::Pass::getAnalysis ( Function F)
inherited

getAnalysis<AnalysisType>() - This function is used by subclasses to get to the analysis information that they claim to use by overriding the getAnalysisUsage function.

Definition at line 228 of file PassAnalysisSupport.h.

228  {
229  assert(Resolver &&"Pass has not been inserted into a PassManager object!");
230 
231  return getAnalysisID<AnalysisType>(&AnalysisType::ID, F);
232 }
F(f)
assert(Globals.size() > 1)
AnalysisResolver * Resolver
Definition: Pass.h:83
template<typename AnalysisType >
AnalysisType & llvm::Pass::getAnalysisID ( AnalysisID  PI) const
inherited

Definition at line 205 of file PassAnalysisSupport.h.

205  {
206  assert(PI && "getAnalysis for unregistered pass!");
207  assert(Resolver&&"Pass has not been inserted into a PassManager object!");
208  // PI *must* appear in AnalysisImpls. Because the number of passes used
209  // should be a small number, we just do a linear search over a (dense)
210  // vector.
211  Pass *ResultPass = Resolver->findImplPass(PI);
212  assert (ResultPass &&
213  "getAnalysis*() called on an analysis that was not "
214  "'required' by pass!");
215 
216  // Because the AnalysisType may not be a subclass of pass (for
217  // AnalysisGroups), we use getAdjustedAnalysisPointer here to potentially
218  // adjust the return pointer (because the class may multiply inherit, once
219  // from pass, once from AnalysisType).
220  return *(AnalysisType*)ResultPass->getAdjustedAnalysisPointer(PI);
221 }
assert(Globals.size() > 1)
Pass * findImplPass(AnalysisID PI)
Pass(const Pass &) LLVM_DELETED_FUNCTION
AnalysisResolver * Resolver
Definition: Pass.h:83
template<typename AnalysisType >
AnalysisType & llvm::Pass::getAnalysisID ( AnalysisID  PI,
Function F 
)
inherited

Definition at line 235 of file PassAnalysisSupport.h.

235  {
236  assert(PI && "getAnalysis for unregistered pass!");
237  assert(Resolver && "Pass has not been inserted into a PassManager object!");
238  // PI *must* appear in AnalysisImpls. Because the number of passes used
239  // should be a small number, we just do a linear search over a (dense)
240  // vector.
241  Pass *ResultPass = Resolver->findImplPass(this, PI, F);
242  assert(ResultPass && "Unable to find requested analysis info");
243 
244  // Because the AnalysisType may not be a subclass of pass (for
245  // AnalysisGroups), we use getAdjustedAnalysisPointer here to potentially
246  // adjust the return pointer (because the class may multiply inherit, once
247  // from pass, once from AnalysisType).
248  return *(AnalysisType*)ResultPass->getAdjustedAnalysisPointer(PI);
249 }
F(f)
assert(Globals.size() > 1)
Pass * findImplPass(AnalysisID PI)
Pass(const Pass &) LLVM_DELETED_FUNCTION
AnalysisResolver * Resolver
Definition: Pass.h:83
template<typename AnalysisType >
AnalysisType * llvm::Pass::getAnalysisIfAvailable ( ) const
inherited

getAnalysisIfAvailable<AnalysisType>() - Subclasses use this function to get analysis information that might be around, for example to update it. This is different than getAnalysis in that it can fail (if the analysis results haven't been computed), so should only be used if you can handle the case when the analysis is not available. This method is often used by transformation APIs to update analysis results for a pass automatically as the transform is performed.

Definition at line 179 of file PassAnalysisSupport.h.

179  {
180  assert(Resolver && "Pass not resident in a PassManager object!");
181 
182  const void *PI = &AnalysisType::ID;
183 
184  Pass *ResultPass = Resolver->getAnalysisIfAvailable(PI, true);
185  if (!ResultPass) return nullptr;
186 
187  // Because the AnalysisType may not be a subclass of pass (for
188  // AnalysisGroups), we use getAdjustedAnalysisPointer here to potentially
189  // adjust the return pointer (because the class may multiply inherit, once
190  // from pass, once from AnalysisType).
191  return (AnalysisType*)ResultPass->getAdjustedAnalysisPointer(PI);
192 }
Pass * getAnalysisIfAvailable(AnalysisID ID, bool Direction) const
assert(Globals.size() > 1)
Pass(const Pass &) LLVM_DELETED_FUNCTION
virtual void * getAdjustedAnalysisPointer(AnalysisID ID)
Definition: Pass.cpp:90
AnalysisResolver * Resolver
Definition: Pass.h:83
Combine redundant false void InstCombiner::getAnalysisUsage ( AnalysisUsage AU) const
overridevirtual

Reimplemented from llvm::Pass.

Definition at line 92 of file InstructionCombining.cpp.

92  {
93  AU.setPreservesCFG();
95 }
AnalysisUsage & addRequired()
void setPreservesCFG()
Definition: Pass.cpp:263
ImmutablePass * Pass::getAsImmutablePass ( )
virtualinherited

Reimplemented in llvm::ImmutablePass.

Definition at line 94 of file Pass.cpp.

94  {
95  return nullptr;
96 }
PMDataManager * Pass::getAsPMDataManager ( )
virtualinherited

Reimplemented in llvm::FPPassManager, llvm::legacy::PassManagerImpl, llvm::legacy::FunctionPassManagerImpl, llvm::LPPassManager, and llvm::RGPassManager.

Definition at line 98 of file Pass.cpp.

98  {
99  return nullptr;
100 }
const DataLayout* llvm::InstCombiner::getDataLayout ( ) const
inline

Definition at line 117 of file InstCombine.h.

117 { return DL; }
const DataLayout * DL
Definition: InstCombine.h:89
AnalysisID llvm::Pass::getPassID ( ) const
inlineinherited

getPassID - Return the PassID number that corresponds to this pass.

Definition at line 104 of file Pass.h.

104  {
105  return PassID;
106  }
const void * PassID
Definition: Pass.h:84
PassKind llvm::Pass::getPassKind ( ) const
inlineinherited

Definition at line 95 of file Pass.h.

95 { return Kind; }
PassKind Kind
Definition: Pass.h:85
const char * Pass::getPassName ( ) const
virtualinherited

getPassName - Return a nice clean name for a pass. This usually implemented in terms of the name that is registered by one of the Registration templates, but can be overloaded directly.

Reimplemented in llvm::FPPassManager, llvm::NVPTXAsmPrinter, llvm::LPPassManager, llvm::RGPassManager, llvm::MipsAsmPrinter, llvm::AMDGPUAsmPrinter, llvm::JumpInstrTables, llvm::ARMAsmPrinter, legup::LegupPass, llvm::DebugIR, llvm::MipsModuleDAGToDAGISel, llvm::JumpInstrTableInfo, llvm::NVPTXLowerAggrCopies, llvm::NVPTXAllocaHoisting, llvm::MachineFunctionAnalysis, llvm::MipsDAGToDAGISel, llvm::Mips16HardFloat, llvm::MipsOs16, llvm::SystemZAsmPrinter, llvm::X86AsmPrinter, and llvm::HexagonAsmPrinter.

Definition at line 61 of file Pass.cpp.

61  {
62  AnalysisID AID = getPassID();
64  if (PI)
65  return PI->getPassName();
66  return "Unnamed pass: implement Pass::getPassName()";
67 }
static PassRegistry * getPassRegistry()
const char * getPassName() const
Definition: PassInfo.h:68
AnalysisID getPassID() const
getPassID - Return the PassID number that corresponds to this pass.
Definition: Pass.h:104
const PassInfo * getPassInfo(const void *TI) const
const void * AnalysisID
Definition: Pass.h:47
PassManagerType FunctionPass::getPotentialPassManagerType ( ) const
overridevirtualinherited

Return what kind of Pass Manager can manage this pass.

Reimplemented from llvm::Pass.

Definition at line 139 of file Pass.cpp.

139  {
141 }
FPPassManager.
Definition: Pass.h:59
AnalysisResolver* llvm::Pass::getResolver ( ) const
inlineinherited

Definition at line 145 of file Pass.h.

145 { return Resolver; }
AnalysisResolver * Resolver
Definition: Pass.h:83
TargetLibraryInfo* llvm::InstCombiner::getTargetLibraryInfo ( ) const
inline

Definition at line 119 of file InstCombine.h.

119 { return TLI; }
TargetLibraryInfo * TLI
Definition: InstCombine.h:90
Instruction* llvm::InstCombiner::InsertNewInstBefore ( Instruction New,
Instruction Old 
)
inline

Definition at line 259 of file InstCombine.h.

259  {
260  assert(New && !New->getParent() &&
261  "New instruction already inserted into a basic block!");
262  BasicBlock *BB = Old.getParent();
263  BB->getInstList().insert(&Old, New); // Insert inst
264  Worklist.Add(New);
265  return New;
266  }
void Add(Instruction *I)
assert(Globals.size() > 1)
InstCombineWorklist Worklist
Worklist - All of the instructions that need to be simplified.
Definition: InstCombine.h:97
Instruction* llvm::InstCombiner::InsertNewInstWith ( Instruction New,
Instruction Old 
)
inline

Definition at line 271 of file InstCombine.h.

271  {
272  New->setDebugLoc(Old.getDebugLoc());
273  return InsertNewInstBefore(New, Old);
274  }
Instruction * InsertNewInstBefore(Instruction *New, Instruction &Old)
Definition: InstCombine.h:259
Value * InstCombiner::InsertRangeTest ( Value V,
Constant Lo,
Constant Hi,
bool  isSigned,
bool  Inside 
)
private

Emit a computation of: (V >= Lo && V < Hi) if Inside is true, otherwise (V < Lo || V >= Hi). In practice, we emit the more efficient (V-Lo) <u Hi-Lo. This method expects that Lo <= Hi. isSigned indicates whether to treat the V, Lo and HI as signed or not. IB is the location to insert new instructions.

Definition at line 263 of file InstCombineAndOrXor.cpp.

264  {
265  assert(cast<ConstantInt>(ConstantExpr::getICmp((isSigned ?
266  ICmpInst::ICMP_SLE:ICmpInst::ICMP_ULE), Lo, Hi))->getZExtValue() &&
267  "Lo is not <= Hi in range emission code!");
268 
269  if (Inside) {
270  if (Lo == Hi) // Trivially false.
271  return Builder->getFalse();
272 
273  // V >= Min && V < Hi --> V < Hi
274  if (cast<ConstantInt>(Lo)->isMinValue(isSigned)) {
275  ICmpInst::Predicate pred = (isSigned ?
277  return Builder->CreateICmp(pred, V, Hi);
278  }
279 
280  // Emit V-Lo <u Hi-Lo
281  Constant *NegLo = ConstantExpr::getNeg(Lo);
282  Value *Add = Builder->CreateAdd(V, NegLo, V->getName()+".off");
283  Constant *UpperBound = ConstantExpr::getAdd(NegLo, Hi);
284  return Builder->CreateICmpULT(Add, UpperBound);
285  }
286 
287  if (Lo == Hi) // Trivially true.
288  return Builder->getTrue();
289 
290  // V < Min || V >= Hi -> V > Hi-1
291  Hi = SubOne(cast<ConstantInt>(Hi));
292  if (cast<ConstantInt>(Lo)->isMinValue(isSigned)) {
293  ICmpInst::Predicate pred = (isSigned ?
295  return Builder->CreateICmp(pred, V, Hi);
296  }
297 
298  // Emit V-Lo >u Hi-1-Lo
299  // Note that Hi has already had one subtracted from it, above.
300  ConstantInt *NegLo = cast<ConstantInt>(ConstantExpr::getNeg(Lo));
301  Value *Add = Builder->CreateAdd(V, NegLo, V->getName()+".off");
302  Constant *LowerBound = ConstantExpr::getAdd(NegLo, Hi);
303  return Builder->CreateICmpUGT(Add, LowerBound);
304 }
Value * CreateICmp(CmpInst::Predicate P, Value *LHS, Value *RHS, const Twine &Name="")
Definition: IRBuilder.h:1321
unsigned less or equal
Definition: InstrTypes.h:698
unsigned less than
Definition: InstrTypes.h:697
Value * CreateICmpULT(Value *LHS, Value *RHS, const Twine &Name="")
Definition: IRBuilder.h:1259
StringRef getName() const
Definition: Value.cpp:168
static Constant * getAdd(Constant *C1, Constant *C2, bool HasNUW=false, bool HasNSW=false)
Definition: Constants.cpp:2102
static Constant * getICmp(unsigned short pred, Constant *LHS, Constant *RHS)
Definition: Constants.cpp:1943
assert(Globals.size() > 1)
ConstantInt * getTrue()
Get the constant value for i1 true.
Definition: IRBuilder.h:256
Value * CreateAdd(Value *LHS, Value *RHS, const Twine &Name="", bool HasNUW=false, bool HasNSW=false)
Definition: IRBuilder.h:632
LLVM Constant Representation.
Definition: Constant.h:41
BuilderTy * Builder
Definition: InstCombine.h:102
signed greater than
Definition: InstrTypes.h:699
Class for constant integers.
Definition: Constants.h:51
signed less than
Definition: InstrTypes.h:701
signed less or equal
Definition: InstrTypes.h:702
ConstantInt * getFalse()
Get the constant value for i1 false.
Definition: IRBuilder.h:261
static Constant * getNeg(Constant *C, bool HasNUW=false, bool HasNSW=false)
Definition: Constants.cpp:2083
LLVM Value Representation.
Definition: Value.h:69
unsigned greater than
Definition: InstrTypes.h:695
Value * CreateICmpUGT(Value *LHS, Value *RHS, const Twine &Name="")
Definition: IRBuilder.h:1253
static Constant * SubOne(Constant *C)
SubOne - Subtract one from a Constant.
Definition: InstCombine.h:64
const PassInfo * Pass::lookupPassInfo ( const void *  TI)
staticinherited

Definition at line 187 of file Pass.cpp.

187  {
189 }
static PassRegistry * getPassRegistry()
const PassInfo * getPassInfo(const void *TI) const
const PassInfo * Pass::lookupPassInfo ( StringRef  Arg)
staticinherited

Definition at line 191 of file Pass.cpp.

191  {
193 }
static PassRegistry * getPassRegistry()
const PassInfo * getPassInfo(const void *TI) const
bool llvm::InstCombiner::MaskedValueIsZero ( Value V,
const APInt Mask,
unsigned  Depth = 0 
) const
inline

Definition at line 323 of file InstCombine.h.

324  {
325  return llvm::MaskedValueIsZero(V, Mask, DL, Depth);
326  }
bool MaskedValueIsZero(Value *V, const APInt &Mask, const DataLayout *TD=nullptr, unsigned Depth=0)
const DataLayout * DL
Definition: InstCombine.h:89
Instruction * InstCombiner::MatchBSwap ( BinaryOperator I)
private

MatchBSwap - Given an OR instruction, check to see if this is a bswap idiom. If so, insert the new bswap intrinsic and return it.

ByteValues - For each byte of the result, we keep track of which value defines each byte.

Definition at line 1501 of file InstCombineAndOrXor.cpp.

1501  {
1502  IntegerType *ITy = dyn_cast<IntegerType>(I.getType());
1503  if (!ITy || ITy->getBitWidth() % 16 ||
1504  // ByteMask only allows up to 32-byte values.
1505  ITy->getBitWidth() > 32*8)
1506  return nullptr; // Can only bswap pairs of bytes. Can't do vectors.
1507 
1508  /// ByteValues - For each byte of the result, we keep track of which value
1509  /// defines each byte.
1510  SmallVector<Value*, 8> ByteValues;
1511  ByteValues.resize(ITy->getBitWidth()/8);
1512 
1513  // Try to find all the pieces corresponding to the bswap.
1514  uint32_t ByteMask = ~0U >> (32-ByteValues.size());
1515  if (CollectBSwapParts(&I, 0, ByteMask, ByteValues))
1516  return nullptr;
1517 
1518  // Check to see if all of the bytes come from the same value.
1519  Value *V = ByteValues[0];
1520  if (!V) return nullptr; // Didn't find a byte? Must be zero.
1521 
1522  // Check to make sure that all of the bytes come from the same value.
1523  for (unsigned i = 1, e = ByteValues.size(); i != e; ++i)
1524  if (ByteValues[i] != V)
1525  return nullptr;
1526  Module *M = I.getParent()->getParent()->getParent();
1527  Function *F = Intrinsic::getDeclaration(M, Intrinsic::bswap, ITy);
1528  return CallInst::Create(F, V);
1529 }
The main container class for the LLVM Intermediate Representation.
Definition: Module.h:115
const Function * getParent() const
Return the enclosing method, or null if none.
Definition: BasicBlock.h:118
F(f)
unsigned getBitWidth() const
Get the number of bits in this IntegerType.
Definition: DerivedTypes.h:61
static CallInst * Create(Value *Func, ArrayRef< Value * > Args, const Twine &NameStr="", Instruction *InsertBefore=nullptr)
Function * getDeclaration(Module *M, ID id, ArrayRef< Type * > Tys=None)
Definition: Function.cpp:737
static bool CollectBSwapParts(Value *V, int OverallLeftShift, uint32_t ByteMask, SmallVectorImpl< Value * > &ByteValues)
Integer representation type.
Definition: DerivedTypes.h:37
Type * getType() const
Definition: Value.h:215
LLVM_ATTRIBUTE_UNUSED_RESULT std::enable_if< !is_simple_type< Y >::value, typename cast_retty< X, const Y >::ret_type >::type dyn_cast(const Y &Val)
Definition: Casting.h:265
void resize(unsigned N)
Definition: SmallVector.h:376
Module * getParent()
Get the module that this global value is contained inside of...
Definition: GlobalValue.h:336
LLVM Value Representation.
Definition: Value.h:69
const BasicBlock * getParent() const
Definition: Instruction.h:53
bool Pass::mustPreserveAnalysisID ( char &  AID) const
inherited

mustPreserveAnalysisID - This method serves the same function as getAnalysisIfAvailable, but works if you just have an AnalysisID. This obviously cannot give you a properly typed instance of the class if you don't have the class name available (use getAnalysisIfAvailable if you do), but it can tell you if you need to preserve the pass at least.

Definition at line 48 of file Pass.cpp.

48  {
49  return Resolver->getAnalysisIfAvailable(&AID, true) != nullptr;
50 }
Pass * getAnalysisIfAvailable(AnalysisID ID, bool Direction) const
AnalysisResolver * Resolver
Definition: Pass.h:83
Instruction * InstCombiner::OptAndOp ( Instruction Op,
ConstantInt OpRHS,
ConstantInt AndRHS,
BinaryOperator TheAnd 
)
private

Definition at line 123 of file InstCombineAndOrXor.cpp.

126  {
127  Value *X = Op->getOperand(0);
128  Constant *Together = nullptr;
129  if (!Op->isShift())
130  Together = ConstantExpr::getAnd(AndRHS, OpRHS);
131 
132  switch (Op->getOpcode()) {
133  case Instruction::Xor:
134  if (Op->hasOneUse()) {
135  // (X ^ C1) & C2 --> (X & C2) ^ (C1&C2)
136  Value *And = Builder->CreateAnd(X, AndRHS);
137  And->takeName(Op);
138  return BinaryOperator::CreateXor(And, Together);
139  }
140  break;
141  case Instruction::Or:
142  if (Op->hasOneUse()){
143  if (Together != OpRHS) {
144  // (X | C1) & C2 --> (X | (C1&C2)) & C2
145  Value *Or = Builder->CreateOr(X, Together);
146  Or->takeName(Op);
147  return BinaryOperator::CreateAnd(Or, AndRHS);
148  }
149 
150  ConstantInt *TogetherCI = dyn_cast<ConstantInt>(Together);
151  if (TogetherCI && !TogetherCI->isZero()){
152  // (X | C1) & C2 --> (X & (C2^(C1&C2))) | C1
153  // NOTE: This reduces the number of bits set in the & mask, which
154  // can expose opportunities for store narrowing.
155  Together = ConstantExpr::getXor(AndRHS, Together);
156  Value *And = Builder->CreateAnd(X, Together);
157  And->takeName(Op);
158  return BinaryOperator::CreateOr(And, OpRHS);
159  }
160  }
161 
162  break;
163  case Instruction::Add:
164  if (Op->hasOneUse()) {
165  // Adding a one to a single bit bit-field should be turned into an XOR
166  // of the bit. First thing to check is to see if this AND is with a
167  // single bit constant.
168  const APInt &AndRHSV = AndRHS->getValue();
169 
170  // If there is only one bit set.
171  if (AndRHSV.isPowerOf2()) {
172  // Ok, at this point, we know that we are masking the result of the
173  // ADD down to exactly one bit. If the constant we are adding has
174  // no bits set below this bit, then we can eliminate the ADD.
175  const APInt& AddRHS = OpRHS->getValue();
176 
177  // Check to see if any bits below the one bit set in AndRHSV are set.
178  if ((AddRHS & (AndRHSV-1)) == 0) {
179  // If not, the only thing that can effect the output of the AND is
180  // the bit specified by AndRHSV. If that bit is set, the effect of
181  // the XOR is to toggle the bit. If it is clear, then the ADD has
182  // no effect.
183  if ((AddRHS & AndRHSV) == 0) { // Bit is not set, noop
184  TheAnd.setOperand(0, X);
185  return &TheAnd;
186  } else {
187  // Pull the XOR out of the AND.
188  Value *NewAnd = Builder->CreateAnd(X, AndRHS);
189  NewAnd->takeName(Op);
190  return BinaryOperator::CreateXor(NewAnd, AndRHS);
191  }
192  }
193  }
194  }
195  break;
196 
197  case Instruction::Shl: {
198  // We know that the AND will not produce any of the bits shifted in, so if
199  // the anded constant includes them, clear them now!
200  //
201  uint32_t BitWidth = AndRHS->getType()->getBitWidth();
202  uint32_t OpRHSVal = OpRHS->getLimitedValue(BitWidth);
203  APInt ShlMask(APInt::getHighBitsSet(BitWidth, BitWidth-OpRHSVal));
204  ConstantInt *CI = Builder->getInt(AndRHS->getValue() & ShlMask);
205 
206  if (CI->getValue() == ShlMask)
207  // Masking out bits that the shift already masks.
208  return ReplaceInstUsesWith(TheAnd, Op); // No need for the and.
209 
210  if (CI != AndRHS) { // Reducing bits set in and.
211  TheAnd.setOperand(1, CI);
212  return &TheAnd;
213  }
214  break;
215  }
216  case Instruction::LShr: {
217  // We know that the AND will not produce any of the bits shifted in, so if
218  // the anded constant includes them, clear them now! This only applies to
219  // unsigned shifts, because a signed shr may bring in set bits!
220  //
221  uint32_t BitWidth = AndRHS->getType()->getBitWidth();
222  uint32_t OpRHSVal = OpRHS->getLimitedValue(BitWidth);
223  APInt ShrMask(APInt::getLowBitsSet(BitWidth, BitWidth - OpRHSVal));
224  ConstantInt *CI = Builder->getInt(AndRHS->getValue() & ShrMask);
225 
226  if (CI->getValue() == ShrMask)
227  // Masking out bits that the shift already masks.
228  return ReplaceInstUsesWith(TheAnd, Op);
229 
230  if (CI != AndRHS) {
231  TheAnd.setOperand(1, CI); // Reduce bits set in and cst.
232  return &TheAnd;
233  }
234  break;
235  }
236  case Instruction::AShr:
237  // Signed shr.
238  // See if this is shifting in some sign extension, then masking it out
239  // with an and.
240  if (Op->hasOneUse()) {
241  uint32_t BitWidth = AndRHS->getType()->getBitWidth();
242  uint32_t OpRHSVal = OpRHS->getLimitedValue(BitWidth);
243  APInt ShrMask(APInt::getLowBitsSet(BitWidth, BitWidth - OpRHSVal));
244  Constant *C = Builder->getInt(AndRHS->getValue() & ShrMask);
245  if (C == AndRHS) { // Masking out bits shifted in.
246  // (Val ashr C1) & C2 -> (Val lshr C1) & C2
247  // Make the argument unsigned.
248  Value *ShVal = Op->getOperand(0);
249  ShVal = Builder->CreateLShr(ShVal, OpRHS, Op->getName());
250  return BinaryOperator::CreateAnd(ShVal, AndRHS, TheAnd.getName());
251  }
252  }
253  break;
254  }
255  return nullptr;
256 }
Value * CreateLShr(Value *LHS, Value *RHS, const Twine &Name="", bool isExact=false)
Definition: IRBuilder.h:770
IntegerType * getType() const
Definition: Constants.h:139
static APInt getLowBitsSet(unsigned numBits, unsigned loBitsSet)
Get a value with low bits set.
Definition: APInt.h:526
static RegisterPass< NVPTXAllocaHoisting > X("alloca-hoisting","Hoisting alloca instructions in non-entry ""blocks to the entry block")
unsigned getBitWidth() const
Get the number of bits in this IntegerType.
Definition: DerivedTypes.h:61
StringRef getName() const
Definition: Value.cpp:168
const APInt & getValue() const
Return the constant's value.
Definition: Constants.h:105
Value * CreateAnd(Value *LHS, Value *RHS, const Twine &Name="")
Definition: IRBuilder.h:806
Value * CreateOr(Value *LHS, Value *RHS, const Twine &Name="")
Definition: IRBuilder.h:822
uint64_t getLimitedValue(uint64_t Limit=~0ULL) const
Get the constant's value with a saturation limit.
Definition: Constants.h:218
void takeName(Value *V)
Definition: Value.cpp:242
static APInt getHighBitsSet(unsigned numBits, unsigned hiBitsSet)
Get a value with high bits set.
Definition: APInt.h:508
LLVM Constant Representation.
Definition: Constant.h:41
Instruction * ReplaceInstUsesWith(Instruction &I, Value *V)
Definition: InstCombine.h:282
static Constant * getAnd(Constant *C1, Constant *C2)
Definition: Constants.cpp:2161
APInt Or(const APInt &LHS, const APInt &RHS)
Bitwise OR function for APInt.
Definition: APInt.h:1870
APInt Xor(const APInt &LHS, const APInt &RHS)
Bitwise XOR function for APInt.
Definition: APInt.h:1875
Value * getOperand(unsigned i) const
Definition: User.h:90
BuilderTy * Builder
Definition: InstCombine.h:102
bool isPowerOf2() const
Check if this APInt's value is a power of two greater than zero.
Definition: APInt.h:388
Class for constant integers.
Definition: Constants.h:51
bool isZero() const
Definition: Constants.h:160
void setOperand(unsigned i, Value *Val)
Definition: User.h:94
Class for arbitrary precision integers.
Definition: APInt.h:75
LLVM_ATTRIBUTE_UNUSED_RESULT std::enable_if< !is_simple_type< Y >::value, typename cast_retty< X, const Y >::ret_type >::type dyn_cast(const Y &Val)
Definition: Casting.h:265
APInt And(const APInt &LHS, const APInt &RHS)
Bitwise AND function for APInt.
Definition: APInt.h:1865
bool hasOneUse() const
Definition: Value.h:284
LLVM Value Representation.
Definition: Value.h:69
unsigned getOpcode() const
getOpcode() returns a member of one of the enums like Instruction::Add.
Definition: Instruction.h:86
ConstantInt * getInt(const APInt &AI)
Get a constant integer value.
Definition: IRBuilder.h:292
static Constant * getXor(Constant *C1, Constant *C2)
Definition: Constants.cpp:2169
Value * InstCombiner::OptimizePointerDifference ( Value LHS,
Value RHS,
Type Ty 
)

Optimize pointer differences into the same array into a size. Consider: &A[10] - &A[0]: we should compile this to "10". LHS/RHS are the pointer operands to the ptrtoint instructions for the LHS/RHS of the subtract.

Definition at line 1384 of file InstCombineAddSub.cpp.

1385  {
1386  assert(DL && "Must have target data info for this");
1387 
1388  // If LHS is a gep based on RHS or RHS is a gep based on LHS, we can optimize
1389  // this.
1390  bool Swapped = false;
1391  GEPOperator *GEP1 = nullptr, *GEP2 = nullptr;
1392 
1393  // For now we require one side to be the base pointer "A" or a constant
1394  // GEP derived from it.
1395  if (GEPOperator *LHSGEP = dyn_cast<GEPOperator>(LHS)) {
1396  // (gep X, ...) - X
1397  if (LHSGEP->getOperand(0) == RHS) {
1398  GEP1 = LHSGEP;
1399  Swapped = false;
1400  } else if (GEPOperator *RHSGEP = dyn_cast<GEPOperator>(RHS)) {
1401  // (gep X, ...) - (gep X, ...)
1402  if (LHSGEP->getOperand(0)->stripPointerCasts() ==
1403  RHSGEP->getOperand(0)->stripPointerCasts()) {
1404  GEP2 = RHSGEP;
1405  GEP1 = LHSGEP;
1406  Swapped = false;
1407  }
1408  }
1409  }
1410 
1411  if (GEPOperator *RHSGEP = dyn_cast<GEPOperator>(RHS)) {
1412  // X - (gep X, ...)
1413  if (RHSGEP->getOperand(0) == LHS) {
1414  GEP1 = RHSGEP;
1415  Swapped = true;
1416  } else if (GEPOperator *LHSGEP = dyn_cast<GEPOperator>(LHS)) {
1417  // (gep X, ...) - (gep X, ...)
1418  if (RHSGEP->getOperand(0)->stripPointerCasts() ==
1419  LHSGEP->getOperand(0)->stripPointerCasts()) {
1420  GEP2 = LHSGEP;
1421  GEP1 = RHSGEP;
1422  Swapped = true;
1423  }
1424  }
1425  }
1426 
1427  // Avoid duplicating the arithmetic if GEP2 has non-constant indices and
1428  // multiple users.
1429  if (!GEP1 ||
1430  (GEP2 && !GEP2->hasAllConstantIndices() && !GEP2->hasOneUse()))
1431  return nullptr;
1432 
1433  // Emit the offset of the GEP and an intptr_t.
1434  Value *Result = EmitGEPOffset(GEP1);
1435 
1436  // If we had a constant expression GEP on the other side offsetting the
1437  // pointer, subtract it from the offset we have.
1438  if (GEP2) {
1439  Value *Offset = EmitGEPOffset(GEP2);
1440  Result = Builder->CreateSub(Result, Offset);
1441  }
1442 
1443  // If we have p - gep(p, ...) then we have to negate the result.
1444  if (Swapped)
1445  Result = Builder->CreateNeg(Result, "diff.neg");
1446 
1447  return Builder->CreateIntCast(Result, Ty, true);
1448 }
Value * CreateIntCast(Value *V, Type *DestTy, bool isSigned, const Twine &Name="")
Definition: IRBuilder.h:1221
Value * CreateSub(Value *LHS, Value *RHS, const Twine &Name="", bool HasNUW=false, bool HasNSW=false)
Definition: IRBuilder.h:654
assert(Globals.size() > 1)
BuilderTy * Builder
Definition: InstCombine.h:102
Value * EmitGEPOffset(User *GEP)
const DataLayout * DL
Definition: InstCombine.h:89
Value * CreateNeg(Value *V, const Twine &Name="", bool HasNUW=false, bool HasNSW=false)
Definition: IRBuilder.h:863
LLVM Value Representation.
Definition: Value.h:69
void Pass::preparePassManager ( PMStack )
virtualinherited

Check if available pass managers are suitable for this pass or not.

Reimplemented in llvm::RegionPass, and llvm::LoopPass.

Definition at line 69 of file Pass.cpp.

69  {
70  // By default, don't do anything.
71 }
void Pass::print ( raw_ostream O,
const Module M 
) const
virtualinherited

print - Print out the internal state of the pass. This is called by Analyze to print out the contents of an analysis. Otherwise it is not necessary to implement this method. Beware that the module pointer MAY be null. This automatically forwards to a virtual function that does not provide the Module* in case the analysis doesn't need it it can just be ignored.

Reimplemented in llvm::DependenceAnalysis, llvm::ScalarEvolution, llvm::RegionInfoPass, llvm::LoopInfo, llvm::CallGraphWrapperPass, llvm::LiveIntervals, llvm::DominanceFrontier, llvm::DominatorTreeWrapperPass, llvm::VirtRegMap, llvm::IVUsers, llvm::MachineDominatorTree, llvm::MachineRegionInfoPass, llvm::LiveStacks, llvm::PostDominatorTree, llvm::MachinePostDominatorTree, llvm::IntervalPartition, llvm::BranchProbabilityInfo, llvm::BlockFrequencyInfo, and llvm::FindUsedTypes.

Definition at line 111 of file Pass.cpp.

111  {
112  O << "Pass::print not implemented for pass: '" << getPassName() << "'!\n";
113 }
virtual const char * getPassName() const
Definition: Pass.cpp:61
Instruction * InstCombiner::PromoteCastOfAllocation ( BitCastInst CI,
AllocaInst AI 
)
private

PromoteCastOfAllocation - If we find a cast of an allocation instruction, try to eliminate the cast by moving the type information into the alloc.

Definition at line 81 of file InstCombineCasts.cpp.

82  {
83  // This requires DataLayout to get the alloca alignment and size information.
84  if (!DL) return nullptr;
85 
86  PointerType *PTy = cast<PointerType>(CI.getType());
87 
88  BuilderTy AllocaBuilder(*Builder);
89  AllocaBuilder.SetInsertPoint(AI.getParent(), &AI);
90 
91  // Get the type really allocated and the type casted to.
92  Type *AllocElTy = AI.getAllocatedType();
93  Type *CastElTy = PTy->getElementType();
94  if (!AllocElTy->isSized() || !CastElTy->isSized()) return nullptr;
95 
96  unsigned AllocElTyAlign = DL->getABITypeAlignment(AllocElTy);
97  unsigned CastElTyAlign = DL->getABITypeAlignment(CastElTy);
98  if (CastElTyAlign < AllocElTyAlign) return nullptr;
99 
100  // If the allocation has multiple uses, only promote it if we are strictly
101  // increasing the alignment of the resultant allocation. If we keep it the
102  // same, we open the door to infinite loops of various kinds.
103  if (!AI.hasOneUse() && CastElTyAlign == AllocElTyAlign) return nullptr;
104 
105  uint64_t AllocElTySize = DL->getTypeAllocSize(AllocElTy);
106  uint64_t CastElTySize = DL->getTypeAllocSize(CastElTy);
107  if (CastElTySize == 0 || AllocElTySize == 0) return nullptr;
108 
109  // If the allocation has multiple uses, only promote it if we're not
110  // shrinking the amount of memory being allocated.
111  uint64_t AllocElTyStoreSize = DL->getTypeStoreSize(AllocElTy);
112  uint64_t CastElTyStoreSize = DL->getTypeStoreSize(CastElTy);
113  if (!AI.hasOneUse() && CastElTyStoreSize < AllocElTyStoreSize) return nullptr;
114 
115  // See if we can satisfy the modulus by pulling a scale out of the array
116  // size argument.
117  unsigned ArraySizeScale;
118  uint64_t ArrayOffset;
119  Value *NumElements = // See if the array size is a decomposable linear expr.
120  DecomposeSimpleLinearExpr(AI.getOperand(0), ArraySizeScale, ArrayOffset);
121 
122  // If we can now satisfy the modulus, by using a non-1 scale, we really can
123  // do the xform.
124  if ((AllocElTySize*ArraySizeScale) % CastElTySize != 0 ||
125  (AllocElTySize*ArrayOffset ) % CastElTySize != 0) return nullptr;
126 
127  unsigned Scale = (AllocElTySize*ArraySizeScale)/CastElTySize;
128  Value *Amt = nullptr;
129  if (Scale == 1) {
130  Amt = NumElements;
131  } else {
132  Amt = ConstantInt::get(AI.getArraySize()->getType(), Scale);
133  // Insert before the alloca, not before the cast.
134  Amt = AllocaBuilder.CreateMul(Amt, NumElements);
135  }
136 
137  if (uint64_t Offset = (AllocElTySize*ArrayOffset)/CastElTySize) {
139  Offset, true);
140  Amt = AllocaBuilder.CreateAdd(Amt, Off);
141  }
142 
143  AllocaInst *New = AllocaBuilder.CreateAlloca(CastElTy, Amt);
144  New->setAlignment(AI.getAlignment());
145  New->takeName(&AI);
147 
148  // If the allocation has multiple real uses, insert a cast and change all
149  // things that used it to use the new cast. This will also hack on CI, but it
150  // will die soon.
151  if (!AI.hasOneUse()) {
152  // New is the allocation instruction, pointer typed. AI is the original
153  // allocation instruction, also pointer typed. Thus, cast to use is BitCast.
154  Value *NewCast = AllocaBuilder.CreateBitCast(New, AI.getType(), "tmpcast");
155  ReplaceInstUsesWith(AI, NewCast);
156  }
157  return ReplaceInstUsesWith(CI, New);
158 }
void setAlignment(unsigned Align)
Type * getAllocatedType() const
void takeName(Value *V)
Definition: Value.cpp:242
Type * getElementType() const
Definition: DerivedTypes.h:319
static Value * DecomposeSimpleLinearExpr(Value *Val, unsigned &Scale, uint64_t &Offset)
PointerType * getType() const
Definition: Instructions.h:93
void setUsedWithInAlloca(bool V)
Specify whether this alloca is used to represent a the arguments to a call.
Definition: Instructions.h:124
Instruction * ReplaceInstUsesWith(Instruction &I, Value *V)
Definition: InstCombine.h:282
unsigned getAlignment() const
Definition: Instructions.h:105
bool isUsedWithInAlloca() const
Return true if this alloca is used as an inalloca argument to a call. Such allocas are never consider...
Definition: Instructions.h:118
Value * getOperand(unsigned i) const
Definition: User.h:90
BuilderTy * Builder
Definition: InstCombine.h:102
unsigned getABITypeAlignment(Type *Ty) const
Definition: DataLayout.cpp:683
uint64_t getTypeAllocSize(Type *Ty) const
Definition: DataLayout.h:368
Type * getType() const
Definition: Value.h:215
static Constant * get(Type *Ty, uint64_t V, bool isSigned=false)
Definition: Constants.cpp:528
bool isSized(SmallPtrSet< const Type *, 4 > *Visited=nullptr) const
Definition: Type.h:268
bool hasOneUse() const
Definition: Value.h:284
uint64_t getTypeStoreSize(Type *Ty) const
Definition: DataLayout.h:353
const DataLayout * DL
Definition: InstCombine.h:89
LLVM Value Representation.
Definition: Value.h:69
const Value * getArraySize() const
Definition: Instructions.h:88
const BasicBlock * getParent() const
Definition: Instruction.h:53
void Pass::releaseMemory ( )
virtualinherited

releaseMemory() - This member can be implemented by a pass if it wants to be able to release its memory when it is no longer needed. The default behavior of passes is to hold onto memory for the entire duration of their lifetime (which is the entire compile time). For pipelined passes, this is not a big deal because that memory gets recycled every time the pass is invoked on another program unit. For IP passes, it is more important to free memory when it is unused.

Optionally implement this function to release pass memory when it is no longer used.

Reimplemented in llvm::DependenceAnalysis, llvm::ScalarEvolution, llvm::RegionInfoPass, llvm::LoopInfo, llvm::CallGraphWrapperPass, llvm::SlotIndexes, llvm::MemoryDependenceAnalysis, llvm::LiveIntervals, llvm::LiveVariables, llvm::DominanceFrontier, llvm::DominatorTreeWrapperPass, llvm::MachineDominatorTree, llvm::SpillPlacement, llvm::IVUsers, llvm::MachineLoopInfo, llvm::MachineRegionInfoPass, llvm::MachineDominanceFrontier, llvm::IntervalPartition, llvm::LiveStacks, llvm::PostDominatorTree, llvm::MachineTraceMetrics, llvm::LazyValueInfo, llvm::LiveDebugVariables, llvm::LiveRegMatrix, llvm::MachineFunctionAnalysis, llvm::MachineBlockFrequencyInfo, and llvm::BlockFrequencyInfo.

Definition at line 82 of file Pass.cpp.

82  {
83  // By default, don't do anything.
84 }
Instruction* llvm::InstCombiner::ReplaceInstUsesWith ( Instruction I,
Value V 
)
inline

Definition at line 282 of file InstCombine.h.

282  {
283  Worklist.AddUsersToWorkList(I); // Add all modified instrs to worklist.
284 
285  // If we are replacing the instruction with itself, this must be in a
286  // segment of unreachable code, so just clobber the instruction.
287  if (&I == V)
288  V = UndefValue::get(I.getType());
289 
290  DEBUG(dbgs() << "IC: Replacing " << I << "\n"
291  " with " << *V << '\n');
292 
293  I.replaceAllUsesWith(V);
294  return &I;
295  }
void AddUsersToWorkList(Instruction &I)
InstCombineWorklist Worklist
Worklist - All of the instructions that need to be simplified.
Definition: InstCombine.h:97
#define DEBUG(X)
static UndefValue * get(Type *T)
Definition: Constants.cpp:1372
raw_ostream & dbgs()
dbgs - Return a circular-buffered debug stream.
#define I(x, y, z)
Definition: MD5.cpp:54
bool InstCombiner::runOnFunction ( Function F)
overridevirtual

Builder - This is an IRBuilder that automatically inserts new instructions into the worklist when they are created.

Implements llvm::FunctionPass.

Definition at line 2828 of file InstructionCombining.cpp.

2828  {
2829  if (skipOptnoneFunction(F))
2830  return false;
2831 
2832  DataLayoutPass *DLP = getAnalysisIfAvailable<DataLayoutPass>();
2833  DL = DLP ? &DLP->getDataLayout() : nullptr;
2834  TLI = &getAnalysis<TargetLibraryInfo>();
2835  // Minimizing size?
2838 
2839  /// Builder - This is an IRBuilder that automatically inserts new
2840  /// instructions into the worklist when they are created.
2842  TheBuilder(F.getContext(), TargetFolder(DL),
2844  Builder = &TheBuilder;
2845 
2846  InstCombinerLibCallSimplifier TheSimplifier(DL, TLI, this);
2847  Simplifier = &TheSimplifier;
2848 
2849  bool EverMadeChange = false;
2850 
2851  // Lower dbg.declare intrinsics otherwise their value may be clobbered
2852  // by instcombiner.
2853  EverMadeChange = LowerDbgDeclare(F);
2854 
2855  // Iterate while there is work to do.
2856  unsigned Iteration = 0;
2857  while (DoOneIteration(F, Iteration++))
2858  EverMadeChange = true;
2859 
2860  Builder = nullptr;
2861  return EverMadeChange;
2862 }
LLVMContext & getContext() const
Definition: Function.cpp:202
bool hasAttribute(unsigned Index, Attribute::AttrKind Kind) const
Return true if the attribute exists at the given index.
Definition: Attributes.cpp:872
TargetLibraryInfo * TLI
Definition: InstCombine.h:90
This provides a uniform API for creating instructions and inserting them into a basic block: either a...
Definition: IRBuilder.h:437
TargetFolder - Create constants with target dependent folding.
Definition: TargetFolder.h:32
const DataLayout & getDataLayout() const
Definition: DataLayout.h:467
InstCombineWorklist Worklist
Worklist - All of the instructions that need to be simplified.
Definition: InstCombine.h:97
BuilderTy * Builder
Definition: InstCombine.h:102
bool LowerDbgDeclare(Function &F)
LibCallSimplifier * Simplifier
Definition: InstCombine.h:92
bool DoOneIteration(Function &F, unsigned ItNum)
AttributeSet getAttributes() const
Return the attribute list for this Function.
Definition: Function.h:170
bool skipOptnoneFunction(const Function &F) const
Definition: Pass.cpp:143
const DataLayout * DL
Definition: InstCombine.h:89
Function must be optimized for size first.
Definition: Attributes.h:79
Instruction * InstCombiner::scalarizePHI ( ExtractElementInst EI,
PHINode PN 
)
private

Definition at line 115 of file InstCombineVectorOps.cpp.

115  {
116  // Verify that the PHI node has exactly 2 uses. Otherwise return NULL.
117  if (!PN->hasNUses(2))
118  return nullptr;
119 
120  // If so, it's known at this point that one operand is PHI and the other is
121  // an extractelement node. Find the PHI user that is not the extractelement
122  // node.
123  auto iu = PN->user_begin();
124  Instruction *PHIUser = dyn_cast<Instruction>(*iu);
125  if (PHIUser == cast<Instruction>(&EI))
126  PHIUser = cast<Instruction>(*(++iu));
127 
128  // Verify that this PHI user has one use, which is the PHI itself,
129  // and that it is a binary operation which is cheap to scalarize.
130  // otherwise return NULL.
131  if (!PHIUser->hasOneUse() || !(PHIUser->user_back() == PN) ||
132  !(isa<BinaryOperator>(PHIUser)) || !CheapToScalarize(PHIUser, true))
133  return nullptr;
134 
135  // Create a scalar PHI node that will replace the vector PHI node
136  // just before the current PHI node.
137  PHINode *scalarPHI = cast<PHINode>(InsertNewInstWith(
138  PHINode::Create(EI.getType(), PN->getNumIncomingValues(), ""), *PN));
139  // Scalarize each PHI operand.
140  for (unsigned i = 0; i < PN->getNumIncomingValues(); i++) {
141  Value *PHIInVal = PN->getIncomingValue(i);
142  BasicBlock *inBB = PN->getIncomingBlock(i);
143  Value *Elt = EI.getIndexOperand();
144  // If the operand is the PHI induction variable:
145  if (PHIInVal == PHIUser) {
146  // Scalarize the binary operation. Its first operand is the
147  // scalar PHI, and the second operand is extracted from the other
148  // vector operand.
149  BinaryOperator *B0 = cast<BinaryOperator>(PHIUser);
150  unsigned opId = (B0->getOperand(0) == PN) ? 1 : 0;
151  Value *Op = InsertNewInstWith(
152  ExtractElementInst::Create(B0->getOperand(opId), Elt,
153  B0->getOperand(opId)->getName() + ".Elt"),
154  *B0);
155  Value *newPHIUser = InsertNewInstWith(
156  BinaryOperator::Create(B0->getOpcode(), scalarPHI, Op), *B0);
157  scalarPHI->addIncoming(newPHIUser, inBB);
158  } else {
159  // Scalarize PHI input:
160  Instruction *newEI = ExtractElementInst::Create(PHIInVal, Elt, "");
161  // Insert the new instruction into the predecessor basic block.
162  Instruction *pos = dyn_cast<Instruction>(PHIInVal);
163  BasicBlock::iterator InsertPos;
164  if (pos && !isa<PHINode>(pos)) {
165  InsertPos = pos;
166  ++InsertPos;
167  } else {
168  InsertPos = inBB->getFirstInsertionPt();
169  }
170 
171  InsertNewInstWith(newEI, *InsertPos);
172 
173  scalarPHI->addIncoming(newEI, inBB);
174  }
175  }
176  return ReplaceInstUsesWith(EI, scalarPHI);
177 }
Instruction * InsertNewInstWith(Instruction *New, Instruction &Old)
Definition: InstCombine.h:271
void addIncoming(Value *V, BasicBlock *BB)
bool hasNUses(unsigned N) const
Definition: Value.cpp:93
StringRef getName() const
Definition: Value.cpp:168
unsigned getNumIncomingValues() const
LLVM Basic Block Representation.
Definition: BasicBlock.h:72
Instruction * ReplaceInstUsesWith(Instruction &I, Value *V)
Definition: InstCombine.h:282
BasicBlock * getIncomingBlock(unsigned i) const
static bool CheapToScalarize(Value *V, bool isConstant)
Value * getOperand(unsigned i) const
Definition: User.h:90
BinaryOps getOpcode() const
Definition: InstrTypes.h:326
Value * getIncomingValue(unsigned i) const
Type * getType() const
Definition: Value.h:215
Instruction * user_back()
Definition: Instruction.h:50
static PHINode * Create(Type *Ty, unsigned NumReservedValues, const Twine &NameStr="", Instruction *InsertBefore=nullptr)
static BinaryOperator * Create(BinaryOps Op, Value *S1, Value *S2, const Twine &Name=Twine(), Instruction *InsertBefore=nullptr)
LLVM_ATTRIBUTE_UNUSED_RESULT std::enable_if< !is_simple_type< Y >::value, typename cast_retty< X, const Y >::ret_type >::type dyn_cast(const Y &Val)
Definition: Casting.h:265
bool hasOneUse() const
Definition: Value.h:284
user_iterator user_begin()
Definition: Value.h:267
LLVM Value Representation.
Definition: Value.h:69
iterator getFirstInsertionPt()
Returns an iterator to the first instruction in this block that is suitable for inserting a non-PHI i...
Definition: BasicBlock.cpp:174
static ExtractElementInst * Create(Value *Vec, Value *Idx, const Twine &NameStr="", Instruction *InsertBefore=nullptr)
void Pass::setResolver ( AnalysisResolver AR)
inherited

Definition at line 102 of file Pass.cpp.

102  {
103  assert(!Resolver && "Resolver is already set");
104  Resolver = AR;
105 }
assert(Globals.size() > 1)
AnalysisResolver * Resolver
Definition: Pass.h:83
bool InstCombiner::ShouldChangeType ( Type From,
Type To 
) const
private

ShouldChangeType - Return true if it is desirable to convert a computation from 'From' to 'To'. We don't want to convert from a legal to an illegal type for example, or from a smaller to a larger illegal type.

Definition at line 105 of file InstructionCombining.cpp.

105  {
106  assert(From->isIntegerTy() && To->isIntegerTy());
107 
108  // If we don't have DL, we don't know if the source/dest are legal.
109  if (!DL) return false;
110 
111  unsigned FromWidth = From->getPrimitiveSizeInBits();
112  unsigned ToWidth = To->getPrimitiveSizeInBits();
113  bool FromLegal = DL->isLegalInteger(FromWidth);
114  bool ToLegal = DL->isLegalInteger(ToWidth);
115 
116  // If this is a legal integer from type, and the result would be an illegal
117  // type, don't do the transformation.
118  if (FromLegal && !ToLegal)
119  return false;
120 
121  // Otherwise, if both are illegal, do not increase the size of the result. We
122  // do allow things like i160 -> i64, but not i64 -> i160.
123  if (!FromLegal && !ToLegal && ToWidth > FromWidth)
124  return false;
125 
126  return true;
127 }
assert(Globals.size() > 1)
bool isIntegerTy() const
Definition: Type.h:193
bool isLegalInteger(unsigned Width) const
Definition: DataLayout.h:224
const DataLayout * DL
Definition: InstCombine.h:89
unsigned getPrimitiveSizeInBits() const LLVM_READONLY
Definition: Type.cpp:117
bool InstCombiner::ShouldOptimizeCast ( Instruction::CastOps  opc,
const Value V,
Type Ty 
)
private

ShouldOptimizeCast - Return true if the cast from "V to Ty" actually results in any code being generated and is interesting to optimize out. If the cast can be eliminated by some other simple transformation, we prefer to do the simplification first.

Definition at line 273 of file InstCombineCasts.cpp.

274  {
275  // Noop casts and casts of constants should be eliminated trivially.
276  if (V->getType() == Ty || isa<Constant>(V)) return false;
277 
278  // If this is another cast that can be eliminated, we prefer to have it
279  // eliminated.
280  if (const CastInst *CI = dyn_cast<CastInst>(V))
281  if (isEliminableCastPair(CI, opc, Ty, DL))
282  return false;
283 
284  // If this is a vector sext from a compare, then we don't want to break the
285  // idiom where each element of the extended vector is either zero or all ones.
286  if (opc == Instruction::SExt && isa<CmpInst>(V) && Ty->isVectorTy())
287  return false;
288 
289  return true;
290 }
Base class of casting instructions.
Definition: InstrTypes.h:387
bool isVectorTy() const
Definition: Type.h:226
Type * getType() const
Definition: Value.h:215
const DataLayout * DL
Definition: InstCombine.h:89
static Instruction::CastOps isEliminableCastPair(const CastInst *CI, unsigned opcode, Type *DstTy, const DataLayout *DL)
bool InstCombiner::SimplifyAssociativeOrCommutative ( BinaryOperator I)
private

SimplifyAssociativeOrCommutative - This performs a few simplifications for operators which are associative or commutative.

SimplifyAssociativeOrCommutative - This performs a few simplifications for operators which are associative or commutative:

Definition at line 203 of file InstructionCombining.cpp.

203  {
204  Instruction::BinaryOps Opcode = I.getOpcode();
205  bool Changed = false;
206 
207  do {
208  // Order operands such that they are listed from right (least complex) to
209  // left (most complex). This puts constants before unary operators before
210  // binary operators.
211  if (I.isCommutative() && getComplexity(I.getOperand(0)) <
213  Changed = !I.swapOperands();
214 
217 
218  if (I.isAssociative()) {
219  // Transform: "(A op B) op C" ==> "A op (B op C)" if "B op C" simplifies.
220  if (Op0 && Op0->getOpcode() == Opcode) {
221  Value *A = Op0->getOperand(0);
222  Value *B = Op0->getOperand(1);
223  Value *C = I.getOperand(1);
224 
225  // Does "B op C" simplify?
226  if (Value *V = SimplifyBinOp(Opcode, B, C, DL)) {
227  // It simplifies to V. Form "A op V".
228  I.setOperand(0, A);
229  I.setOperand(1, V);
230  // Conservatively clear the optional flags, since they may not be
231  // preserved by the reassociation.
232  if (MaintainNoSignedWrap(I, B, C) &&
233  (!Op0 || (isa<BinaryOperator>(Op0) && Op0->hasNoSignedWrap()))) {
234  // Note: this is only valid because SimplifyBinOp doesn't look at
235  // the operands to Op0.
237  I.setHasNoSignedWrap(true);
238  } else {
240  }
241 
242  Changed = true;
243  ++NumReassoc;
244  continue;
245  }
246  }
247 
248  // Transform: "A op (B op C)" ==> "(A op B) op C" if "A op B" simplifies.
249  if (Op1 && Op1->getOpcode() == Opcode) {
250  Value *A = I.getOperand(0);
251  Value *B = Op1->getOperand(0);
252  Value *C = Op1->getOperand(1);
253 
254  // Does "A op B" simplify?
255  if (Value *V = SimplifyBinOp(Opcode, A, B, DL)) {
256  // It simplifies to V. Form "V op C".
257  I.setOperand(0, V);
258  I.setOperand(1, C);
259  // Conservatively clear the optional flags, since they may not be
260  // preserved by the reassociation.
262  Changed = true;
263  ++NumReassoc;
264  continue;
265  }
266  }
267  }
268 
269  if (I.isAssociative() && I.isCommutative()) {
270  // Transform: "(A op B) op C" ==> "(C op A) op B" if "C op A" simplifies.
271  if (Op0 && Op0->getOpcode() == Opcode) {
272  Value *A = Op0->getOperand(0);
273  Value *B = Op0->getOperand(1);
274  Value *C = I.getOperand(1);
275 
276  // Does "C op A" simplify?
277  if (Value *V = SimplifyBinOp(Opcode, C, A, DL)) {
278  // It simplifies to V. Form "V op B".
279  I.setOperand(0, V);
280  I.setOperand(1, B);
281  // Conservatively clear the optional flags, since they may not be
282  // preserved by the reassociation.
284  Changed = true;
285  ++NumReassoc;
286  continue;
287  }
288  }
289 
290  // Transform: "A op (B op C)" ==> "B op (C op A)" if "C op A" simplifies.
291  if (Op1 && Op1->getOpcode() == Opcode) {
292  Value *A = I.getOperand(0);
293  Value *B = Op1->getOperand(0);
294  Value *C = Op1->getOperand(1);
295 
296  // Does "C op A" simplify?
297  if (Value *V = SimplifyBinOp(Opcode, C, A, DL)) {
298  // It simplifies to V. Form "B op V".
299  I.setOperand(0, B);
300  I.setOperand(1, V);
301  // Conservatively clear the optional flags, since they may not be
302  // preserved by the reassociation.
304  Changed = true;
305  ++NumReassoc;
306  continue;
307  }
308  }
309 
310  // Transform: "(A op C1) op (B op C2)" ==> "(A op B) op (C1 op C2)"
311  // if C1 and C2 are constants.
312  if (Op0 && Op1 &&
313  Op0->getOpcode() == Opcode && Op1->getOpcode() == Opcode &&
314  isa<Constant>(Op0->getOperand(1)) &&
315  isa<Constant>(Op1->getOperand(1)) &&
316  Op0->hasOneUse() && Op1->hasOneUse()) {
317  Value *A = Op0->getOperand(0);
318  Constant *C1 = cast<Constant>(Op0->getOperand(1));
319  Value *B = Op1->getOperand(0);
320  Constant *C2 = cast<Constant>(Op1->getOperand(1));
321 
322  Constant *Folded = ConstantExpr::get(Opcode, C1, C2);
323  BinaryOperator *New = BinaryOperator::Create(Opcode, A, B);
324  if (isa<FPMathOperator>(New)) {
326  Flags &= Op0->getFastMathFlags();
327  Flags &= Op1->getFastMathFlags();
328  New->setFastMathFlags(Flags);
329  }
330  InsertNewInstWith(New, I);
331  New->takeName(Op1);
332  I.setOperand(0, New);
333  I.setOperand(1, Folded);
334  // Conservatively clear the optional flags, since they may not be
335  // preserved by the reassociation.
337 
338  Changed = true;
339  continue;
340  }
341  }
342 
343  // No further simplifications.
344  return Changed;
345  } while (1);
346 }
Instruction * InsertNewInstWith(Instruction *New, Instruction &Old)
Definition: InstCombine.h:271
void setHasNoSignedWrap(bool b=true)
void setFastMathFlags(FastMathFlags FMF)
static void ClearSubclassDataAfterReassociation(BinaryOperator &I)
FastMathFlags getFastMathFlags() const
static bool MaintainNoSignedWrap(BinaryOperator &I, Value *B, Value *C)
static unsigned getComplexity(Value *V)
Definition: InstCombine.h:47
static Constant * get(unsigned Opcode, Constant *C1, Constant *C2, unsigned Flags=0)
Definition: Constants.cpp:1752
bool isAssociative() const
void takeName(Value *V)
Definition: Value.cpp:242
LLVM Constant Representation.
Definition: Constant.h:41
Value * getOperand(unsigned i) const
Definition: User.h:90
bool isCommutative() const
Definition: Instruction.h:287
bool hasNoSignedWrap() const
hasNoSignedWrap - Determine whether the no signed wrap flag is set.
BinaryOps getOpcode() const
Definition: InstrTypes.h:326
Definition: test.h:1
Value * SimplifyBinOp(unsigned Opcode, Value *LHS, Value *RHS, const DataLayout *TD=nullptr, const TargetLibraryInfo *TLI=nullptr, const DominatorTree *DT=nullptr)
void setOperand(unsigned i, Value *Val)
Definition: User.h:94
static BinaryOperator * Create(BinaryOps Op, Value *S1, Value *S2, const Twine &Name=Twine(), Instruction *InsertBefore=nullptr)
LLVM_ATTRIBUTE_UNUSED_RESULT std::enable_if< !is_simple_type< Y >::value, typename cast_retty< X, const Y >::ret_type >::type dyn_cast(const Y &Val)
Definition: Casting.h:265
bool hasOneUse() const
Definition: Value.h:284
const DataLayout * DL
Definition: InstCombine.h:89
LLVM Value Representation.
Definition: Value.h:69
void clearSubclassOptionalData()
Definition: Value.h:366
Convenience struct for specifying and reasoning about fast-math flags.
Definition: Operator.h:170
bool InstCombiner::SimplifyDemandedBits ( Use U,
APInt  DemandedMask,
APInt KnownZero,
APInt KnownOne,
unsigned  Depth = 0 
)
private

SimplifyDemandedBits - This form of SimplifyDemandedBits simplifies the specified instruction operand if possible, updating it in place. It returns true if it made any change and false otherwise.

Definition at line 70 of file InstCombineSimplifyDemanded.cpp.

72  {
73  Value *NewVal = SimplifyDemandedUseBits(U.get(), DemandedMask,
74  KnownZero, KnownOne, Depth);
75  if (!NewVal) return false;
76  U = NewVal;
77  return true;
78 }
Value * SimplifyDemandedUseBits(Value *V, APInt DemandedMask, APInt &KnownZero, APInt &KnownOne, unsigned Depth)
Value * get() const
Definition: Use.h:95
LLVM Value Representation.
Definition: Value.h:69
bool InstCombiner::SimplifyDemandedInstructionBits ( Instruction Inst)
private

SimplifyDemandedInstructionBits - Inst is an integer instruction that SimplifyDemandedBits knows about. See if the instruction has any properties that allow us to simplify its operands.

Definition at line 54 of file InstCombineSimplifyDemanded.cpp.

54  {
55  unsigned BitWidth = Inst.getType()->getScalarSizeInBits();
56  APInt KnownZero(BitWidth, 0), KnownOne(BitWidth, 0);
57  APInt DemandedMask(APInt::getAllOnesValue(BitWidth));
58 
59  Value *V = SimplifyDemandedUseBits(&Inst, DemandedMask,
60  KnownZero, KnownOne, 0);
61  if (!V) return false;
62  if (V == &Inst) return true;
63  ReplaceInstUsesWith(Inst, V);
64  return true;
65 }
static APInt getAllOnesValue(unsigned numBits)
Get the all-ones value.
Definition: APInt.h:448
Value * SimplifyDemandedUseBits(Value *V, APInt DemandedMask, APInt &KnownZero, APInt &KnownOne, unsigned Depth)
Instruction * ReplaceInstUsesWith(Instruction &I, Value *V)
Definition: InstCombine.h:282
unsigned getScalarSizeInBits() const LLVM_READONLY
Definition: Type.cpp:135
Type * getType() const
Definition: Value.h:215
Class for arbitrary precision integers.
Definition: APInt.h:75
LLVM Value Representation.
Definition: Value.h:69
Value * InstCombiner::SimplifyDemandedUseBits ( Value V,
APInt  DemandedMask,
APInt KnownZero,
APInt KnownOne,
unsigned  Depth 
)
private

SimplifyDemandedUseBits - Attempts to replace V with a simpler value based on the demanded bits.

SimplifyDemandedUseBits - This function attempts to replace V with a simpler value based on the demanded bits. When this function is called, it is known that only the bits set in DemandedMask of the result of V are ever used downstream. Consequently, depending on the mask and V, it may be possible to replace V with a constant or one of its operands. In such cases, this function does the replacement and returns true. In all other cases, it returns false after analyzing the expression and setting KnownOne and known to be one in the expression. KnownZero contains all the bits that are known to be zero in the expression. These are provided to potentially allow the caller (which might recursively be SimplifyDemandedBits itself) to simplify the expression. KnownOne and KnownZero always follow the invariant that KnownOne & KnownZero == 0. That is, a bit can't be both 1 and 0. Note that the bits in KnownOne and KnownZero may only be accurate for those bits set in DemandedMask. Note also that the bitwidth of V, DemandedMask, KnownZero and KnownOne must all be the same.

This returns null if it did not change anything and it permits no simplification. This returns V itself if it did some simplification of V's operands based on the information about what bits are demanded. This returns some other non-null value if it found out that V is equal to another value in the context where the specified bits are demanded, but not for all users.

Definition at line 102 of file InstCombineSimplifyDemanded.cpp.

104  {
105  assert(V != nullptr && "Null pointer of Value???");
106  assert(Depth <= 6 && "Limit Search Depth");
107  uint32_t BitWidth = DemandedMask.getBitWidth();
108  Type *VTy = V->getType();
109  assert((DL || !VTy->isPointerTy()) &&
110  "SimplifyDemandedBits needs to know bit widths!");
111  assert((!DL || DL->getTypeSizeInBits(VTy->getScalarType()) == BitWidth) &&
112  (!VTy->isIntOrIntVectorTy() ||
113  VTy->getScalarSizeInBits() == BitWidth) &&
114  KnownZero.getBitWidth() == BitWidth &&
115  KnownOne.getBitWidth() == BitWidth &&
116  "Value *V, DemandedMask, KnownZero and KnownOne "
117  "must have same BitWidth");
118  if (ConstantInt *CI = dyn_cast<ConstantInt>(V)) {
119  // We know all of the bits for a constant!
120  KnownOne = CI->getValue() & DemandedMask;
121  KnownZero = ~KnownOne & DemandedMask;
122  return nullptr;
123  }
124  if (isa<ConstantPointerNull>(V)) {
125  // We know all of the bits for a constant!
126  KnownOne.clearAllBits();
127  KnownZero = DemandedMask;
128  return nullptr;
129  }
130 
131  KnownZero.clearAllBits();
132  KnownOne.clearAllBits();
133  if (DemandedMask == 0) { // Not demanding any bits from V.
134  if (isa<UndefValue>(V))
135  return nullptr;
136  return UndefValue::get(VTy);
137  }
138 
139  if (Depth == 6) // Limit search depth.
140  return nullptr;
141 
142  APInt LHSKnownZero(BitWidth, 0), LHSKnownOne(BitWidth, 0);
143  APInt RHSKnownZero(BitWidth, 0), RHSKnownOne(BitWidth, 0);
144 
146  if (!I) {
147  computeKnownBits(V, KnownZero, KnownOne, Depth);
148  return nullptr; // Only analyze instructions.
149  }
150 
151  // If there are multiple uses of this value and we aren't at the root, then
152  // we can't do any simplifications of the operands, because DemandedMask
153  // only reflects the bits demanded by *one* of the users.
154  if (Depth != 0 && !I->hasOneUse()) {
155  // Despite the fact that we can't simplify this instruction in all User's
156  // context, we can at least compute the knownzero/knownone bits, and we can
157  // do simplifications that apply to *just* the one user if we know that
158  // this instruction has a simpler value in that context.
159  if (I->getOpcode() == Instruction::And) {
160  // If either the LHS or the RHS are Zero, the result is zero.
161  computeKnownBits(I->getOperand(1), RHSKnownZero, RHSKnownOne, Depth+1);
162  computeKnownBits(I->getOperand(0), LHSKnownZero, LHSKnownOne, Depth+1);
163 
164  // If all of the demanded bits are known 1 on one side, return the other.
165  // These bits cannot contribute to the result of the 'and' in this
166  // context.
167  if ((DemandedMask & ~LHSKnownZero & RHSKnownOne) ==
168  (DemandedMask & ~LHSKnownZero))
169  return I->getOperand(0);
170  if ((DemandedMask & ~RHSKnownZero & LHSKnownOne) ==
171  (DemandedMask & ~RHSKnownZero))
172  return I->getOperand(1);
173 
174  // If all of the demanded bits in the inputs are known zeros, return zero.
175  if ((DemandedMask & (RHSKnownZero|LHSKnownZero)) == DemandedMask)
176  return Constant::getNullValue(VTy);
177 
178  } else if (I->getOpcode() == Instruction::Or) {
179  // We can simplify (X|Y) -> X or Y in the user's context if we know that
180  // only bits from X or Y are demanded.
181 
182  // If either the LHS or the RHS are One, the result is One.
183  computeKnownBits(I->getOperand(1), RHSKnownZero, RHSKnownOne, Depth+1);
184  computeKnownBits(I->getOperand(0), LHSKnownZero, LHSKnownOne, Depth+1);
185 
186  // If all of the demanded bits are known zero on one side, return the
187  // other. These bits cannot contribute to the result of the 'or' in this
188  // context.
189  if ((DemandedMask & ~LHSKnownOne & RHSKnownZero) ==
190  (DemandedMask & ~LHSKnownOne))
191  return I->getOperand(0);
192  if ((DemandedMask & ~RHSKnownOne & LHSKnownZero) ==
193  (DemandedMask & ~RHSKnownOne))
194  return I->getOperand(1);
195 
196  // If all of the potentially set bits on one side are known to be set on
197  // the other side, just use the 'other' side.
198  if ((DemandedMask & (~RHSKnownZero) & LHSKnownOne) ==
199  (DemandedMask & (~RHSKnownZero)))
200  return I->getOperand(0);
201  if ((DemandedMask & (~LHSKnownZero) & RHSKnownOne) ==
202  (DemandedMask & (~LHSKnownZero)))
203  return I->getOperand(1);
204  } else if (I->getOpcode() == Instruction::Xor) {
205  // We can simplify (X^Y) -> X or Y in the user's context if we know that
206  // only bits from X or Y are demanded.
207 
208  computeKnownBits(I->getOperand(1), RHSKnownZero, RHSKnownOne, Depth+1);
209  computeKnownBits(I->getOperand(0), LHSKnownZero, LHSKnownOne, Depth+1);
210 
211  // If all of the demanded bits are known zero on one side, return the
212  // other.
213  if ((DemandedMask & RHSKnownZero) == DemandedMask)
214  return I->getOperand(0);
215  if ((DemandedMask & LHSKnownZero) == DemandedMask)
216  return I->getOperand(1);
217  }
218 
219  // Compute the KnownZero/KnownOne bits to simplify things downstream.
220  computeKnownBits(I, KnownZero, KnownOne, Depth);
221  return nullptr;
222  }
223 
224  // If this is the root being simplified, allow it to have multiple uses,
225  // just set the DemandedMask to all bits so that we can try to simplify the
226  // operands. This allows visitTruncInst (for example) to simplify the
227  // operand of a trunc without duplicating all the logic below.
228  if (Depth == 0 && !V->hasOneUse())
229  DemandedMask = APInt::getAllOnesValue(BitWidth);
230 
231  switch (I->getOpcode()) {
232  default:
233  computeKnownBits(I, KnownZero, KnownOne, Depth);
234  break;
235  case Instruction::And:
236  // If either the LHS or the RHS are Zero, the result is zero.
237  if (SimplifyDemandedBits(I->getOperandUse(1), DemandedMask,
238  RHSKnownZero, RHSKnownOne, Depth+1) ||
239  SimplifyDemandedBits(I->getOperandUse(0), DemandedMask & ~RHSKnownZero,
240  LHSKnownZero, LHSKnownOne, Depth+1))
241  return I;
242  assert(!(RHSKnownZero & RHSKnownOne) && "Bits known to be one AND zero?");
243  assert(!(LHSKnownZero & LHSKnownOne) && "Bits known to be one AND zero?");
244 
245  // If all of the demanded bits are known 1 on one side, return the other.
246  // These bits cannot contribute to the result of the 'and'.
247  if ((DemandedMask & ~LHSKnownZero & RHSKnownOne) ==
248  (DemandedMask & ~LHSKnownZero))
249  return I->getOperand(0);
250  if ((DemandedMask & ~RHSKnownZero & LHSKnownOne) ==
251  (DemandedMask & ~RHSKnownZero))
252  return I->getOperand(1);
253 
254  // If all of the demanded bits in the inputs are known zeros, return zero.
255  if ((DemandedMask & (RHSKnownZero|LHSKnownZero)) == DemandedMask)
256  return Constant::getNullValue(VTy);
257 
258  // If the RHS is a constant, see if we can simplify it.
259  if (ShrinkDemandedConstant(I, 1, DemandedMask & ~LHSKnownZero))
260  return I;
261 
262  // Output known-1 bits are only known if set in both the LHS & RHS.
263  KnownOne = RHSKnownOne & LHSKnownOne;
264  // Output known-0 are known to be clear if zero in either the LHS | RHS.
265  KnownZero = RHSKnownZero | LHSKnownZero;
266  break;
267  case Instruction::Or:
268  // If either the LHS or the RHS are One, the result is One.
269  if (SimplifyDemandedBits(I->getOperandUse(1), DemandedMask,
270  RHSKnownZero, RHSKnownOne, Depth+1) ||
271  SimplifyDemandedBits(I->getOperandUse(0), DemandedMask & ~RHSKnownOne,
272  LHSKnownZero, LHSKnownOne, Depth+1))
273  return I;
274  assert(!(RHSKnownZero & RHSKnownOne) && "Bits known to be one AND zero?");
275  assert(!(LHSKnownZero & LHSKnownOne) && "Bits known to be one AND zero?");
276 
277  // If all of the demanded bits are known zero on one side, return the other.
278  // These bits cannot contribute to the result of the 'or'.
279  if ((DemandedMask & ~LHSKnownOne & RHSKnownZero) ==
280  (DemandedMask & ~LHSKnownOne))
281  return I->getOperand(0);
282  if ((DemandedMask & ~RHSKnownOne & LHSKnownZero) ==
283  (DemandedMask & ~RHSKnownOne))
284  return I->getOperand(1);
285 
286  // If all of the potentially set bits on one side are known to be set on
287  // the other side, just use the 'other' side.
288  if ((DemandedMask & (~RHSKnownZero) & LHSKnownOne) ==
289  (DemandedMask & (~RHSKnownZero)))
290  return I->getOperand(0);
291  if ((DemandedMask & (~LHSKnownZero) & RHSKnownOne) ==
292  (DemandedMask & (~LHSKnownZero)))
293  return I->getOperand(1);
294 
295  // If the RHS is a constant, see if we can simplify it.
296  if (ShrinkDemandedConstant(I, 1, DemandedMask))
297  return I;
298 
299  // Output known-0 bits are only known if clear in both the LHS & RHS.
300  KnownZero = RHSKnownZero & LHSKnownZero;
301  // Output known-1 are known to be set if set in either the LHS | RHS.
302  KnownOne = RHSKnownOne | LHSKnownOne;
303  break;
304  case Instruction::Xor: {
305  if (SimplifyDemandedBits(I->getOperandUse(1), DemandedMask,
306  RHSKnownZero, RHSKnownOne, Depth+1) ||
307  SimplifyDemandedBits(I->getOperandUse(0), DemandedMask,
308  LHSKnownZero, LHSKnownOne, Depth+1))
309  return I;
310  assert(!(RHSKnownZero & RHSKnownOne) && "Bits known to be one AND zero?");
311  assert(!(LHSKnownZero & LHSKnownOne) && "Bits known to be one AND zero?");
312 
313  // If all of the demanded bits are known zero on one side, return the other.
314  // These bits cannot contribute to the result of the 'xor'.
315  if ((DemandedMask & RHSKnownZero) == DemandedMask)
316  return I->getOperand(0);
317  if ((DemandedMask & LHSKnownZero) == DemandedMask)
318  return I->getOperand(1);
319 
320  // If all of the demanded bits are known to be zero on one side or the
321  // other, turn this into an *inclusive* or.
322  // e.g. (A & C1)^(B & C2) -> (A & C1)|(B & C2) iff C1&C2 == 0
323  if ((DemandedMask & ~RHSKnownZero & ~LHSKnownZero) == 0) {
324  Instruction *Or =
325  BinaryOperator::CreateOr(I->getOperand(0), I->getOperand(1),
326  I->getName());
327  return InsertNewInstWith(Or, *I);
328  }
329 
330  // If all of the demanded bits on one side are known, and all of the set
331  // bits on that side are also known to be set on the other side, turn this
332  // into an AND, as we know the bits will be cleared.
333  // e.g. (X | C1) ^ C2 --> (X | C1) & ~C2 iff (C1&C2) == C2
334  if ((DemandedMask & (RHSKnownZero|RHSKnownOne)) == DemandedMask) {
335  // all known
336  if ((RHSKnownOne & LHSKnownOne) == RHSKnownOne) {
338  ~RHSKnownOne & DemandedMask);
339  Instruction *And = BinaryOperator::CreateAnd(I->getOperand(0), AndC);
340  return InsertNewInstWith(And, *I);
341  }
342  }
343 
344  // If the RHS is a constant, see if we can simplify it.
345  // FIXME: for XOR, we prefer to force bits to 1 if they will make a -1.
346  if (ShrinkDemandedConstant(I, 1, DemandedMask))
347  return I;
348 
349  // If our LHS is an 'and' and if it has one use, and if any of the bits we
350  // are flipping are known to be set, then the xor is just resetting those
351  // bits to zero. We can just knock out bits from the 'and' and the 'xor',
352  // simplifying both of them.
353  if (Instruction *LHSInst = dyn_cast<Instruction>(I->getOperand(0)))
354  if (LHSInst->getOpcode() == Instruction::And && LHSInst->hasOneUse() &&
355  isa<ConstantInt>(I->getOperand(1)) &&
356  isa<ConstantInt>(LHSInst->getOperand(1)) &&
357  (LHSKnownOne & RHSKnownOne & DemandedMask) != 0) {
358  ConstantInt *AndRHS = cast<ConstantInt>(LHSInst->getOperand(1));
359  ConstantInt *XorRHS = cast<ConstantInt>(I->getOperand(1));
360  APInt NewMask = ~(LHSKnownOne & RHSKnownOne & DemandedMask);
361 
362  Constant *AndC =
363  ConstantInt::get(I->getType(), NewMask & AndRHS->getValue());
364  Instruction *NewAnd = BinaryOperator::CreateAnd(I->getOperand(0), AndC);
365  InsertNewInstWith(NewAnd, *I);
366 
367  Constant *XorC =
368  ConstantInt::get(I->getType(), NewMask & XorRHS->getValue());
369  Instruction *NewXor = BinaryOperator::CreateXor(NewAnd, XorC);
370  return InsertNewInstWith(NewXor, *I);
371  }
372 
373  // Output known-0 bits are known if clear or set in both the LHS & RHS.
374  KnownZero= (RHSKnownZero & LHSKnownZero) | (RHSKnownOne & LHSKnownOne);
375  // Output known-1 are known to be set if set in only one of the LHS, RHS.
376  KnownOne = (RHSKnownZero & LHSKnownOne) | (RHSKnownOne & LHSKnownZero);
377  break;
378  }
379  case Instruction::Select:
380  if (SimplifyDemandedBits(I->getOperandUse(2), DemandedMask,
381  RHSKnownZero, RHSKnownOne, Depth+1) ||
382  SimplifyDemandedBits(I->getOperandUse(1), DemandedMask,
383  LHSKnownZero, LHSKnownOne, Depth+1))
384  return I;
385  assert(!(RHSKnownZero & RHSKnownOne) && "Bits known to be one AND zero?");
386  assert(!(LHSKnownZero & LHSKnownOne) && "Bits known to be one AND zero?");
387 
388  // If the operands are constants, see if we can simplify them.
389  if (ShrinkDemandedConstant(I, 1, DemandedMask) ||
390  ShrinkDemandedConstant(I, 2, DemandedMask))
391  return I;
392 
393  // Only known if known in both the LHS and RHS.
394  KnownOne = RHSKnownOne & LHSKnownOne;
395  KnownZero = RHSKnownZero & LHSKnownZero;
396  break;
397  case Instruction::Trunc: {
398  unsigned truncBf = I->getOperand(0)->getType()->getScalarSizeInBits();
399  DemandedMask = DemandedMask.zext(truncBf);
400  KnownZero = KnownZero.zext(truncBf);
401  KnownOne = KnownOne.zext(truncBf);
402  if (SimplifyDemandedBits(I->getOperandUse(0), DemandedMask,
403  KnownZero, KnownOne, Depth+1))
404  return I;
405  DemandedMask = DemandedMask.trunc(BitWidth);
406  KnownZero = KnownZero.trunc(BitWidth);
407  KnownOne = KnownOne.trunc(BitWidth);
408  assert(!(KnownZero & KnownOne) && "Bits known to be one AND zero?");
409  break;
410  }
411  case Instruction::BitCast:
412  if (!I->getOperand(0)->getType()->isIntOrIntVectorTy())
413  return nullptr; // vector->int or fp->int?
414 
415  if (VectorType *DstVTy = dyn_cast<VectorType>(I->getType())) {
416  if (VectorType *SrcVTy =
417  dyn_cast<VectorType>(I->getOperand(0)->getType())) {
418  if (DstVTy->getNumElements() != SrcVTy->getNumElements())
419  // Don't touch a bitcast between vectors of different element counts.
420  return nullptr;
421  } else
422  // Don't touch a scalar-to-vector bitcast.
423  return nullptr;
424  } else if (I->getOperand(0)->getType()->isVectorTy())
425  // Don't touch a vector-to-scalar bitcast.
426  return nullptr;
427 
428  if (SimplifyDemandedBits(I->getOperandUse(0), DemandedMask,
429  KnownZero, KnownOne, Depth+1))
430  return I;
431  assert(!(KnownZero & KnownOne) && "Bits known to be one AND zero?");
432  break;
433  case Instruction::ZExt: {
434  // Compute the bits in the result that are not present in the input.
435  unsigned SrcBitWidth =I->getOperand(0)->getType()->getScalarSizeInBits();
436 
437  DemandedMask = DemandedMask.trunc(SrcBitWidth);
438  KnownZero = KnownZero.trunc(SrcBitWidth);
439  KnownOne = KnownOne.trunc(SrcBitWidth);
440  if (SimplifyDemandedBits(I->getOperandUse(0), DemandedMask,
441  KnownZero, KnownOne, Depth+1))
442  return I;
443  DemandedMask = DemandedMask.zext(BitWidth);
444  KnownZero = KnownZero.zext(BitWidth);
445  KnownOne = KnownOne.zext(BitWidth);
446  assert(!(KnownZero & KnownOne) && "Bits known to be one AND zero?");
447  // The top bits are known to be zero.
448  KnownZero |= APInt::getHighBitsSet(BitWidth, BitWidth - SrcBitWidth);
449  break;
450  }
451  case Instruction::SExt: {
452  // Compute the bits in the result that are not present in the input.
453  unsigned SrcBitWidth =I->getOperand(0)->getType()->getScalarSizeInBits();
454 
455  APInt InputDemandedBits = DemandedMask &
456  APInt::getLowBitsSet(BitWidth, SrcBitWidth);
457 
458  APInt NewBits(APInt::getHighBitsSet(BitWidth, BitWidth - SrcBitWidth));
459  // If any of the sign extended bits are demanded, we know that the sign
460  // bit is demanded.
461  if ((NewBits & DemandedMask) != 0)
462  InputDemandedBits.setBit(SrcBitWidth-1);
463 
464  InputDemandedBits = InputDemandedBits.trunc(SrcBitWidth);
465  KnownZero = KnownZero.trunc(SrcBitWidth);
466  KnownOne = KnownOne.trunc(SrcBitWidth);
467  if (SimplifyDemandedBits(I->getOperandUse(0), InputDemandedBits,
468  KnownZero, KnownOne, Depth+1))
469  return I;
470  InputDemandedBits = InputDemandedBits.zext(BitWidth);
471  KnownZero = KnownZero.zext(BitWidth);
472  KnownOne = KnownOne.zext(BitWidth);
473  assert(!(KnownZero & KnownOne) && "Bits known to be one AND zero?");
474 
475  // If the sign bit of the input is known set or clear, then we know the
476  // top bits of the result.
477 
478  // If the input sign bit is known zero, or if the NewBits are not demanded
479  // convert this into a zero extension.
480  if (KnownZero[SrcBitWidth-1] || (NewBits & ~DemandedMask) == NewBits) {
481  // Convert to ZExt cast
482  CastInst *NewCast = new ZExtInst(I->getOperand(0), VTy, I->getName());
483  return InsertNewInstWith(NewCast, *I);
484  } else if (KnownOne[SrcBitWidth-1]) { // Input sign bit known set
485  KnownOne |= NewBits;
486  }
487  break;
488  }
489  case Instruction::Add: {
490  // Figure out what the input bits are. If the top bits of the and result
491  // are not demanded, then the add doesn't demand them from its input
492  // either.
493  unsigned NLZ = DemandedMask.countLeadingZeros();
494 
495  // If there is a constant on the RHS, there are a variety of xformations
496  // we can do.
497  if (ConstantInt *RHS = dyn_cast<ConstantInt>(I->getOperand(1))) {
498  // If null, this should be simplified elsewhere. Some of the xforms here
499  // won't work if the RHS is zero.
500  if (RHS->isZero())
501  break;
502 
503  // If the top bit of the output is demanded, demand everything from the
504  // input. Otherwise, we demand all the input bits except NLZ top bits.
505  APInt InDemandedBits(APInt::getLowBitsSet(BitWidth, BitWidth - NLZ));
506 
507  // Find information about known zero/one bits in the input.
508  if (SimplifyDemandedBits(I->getOperandUse(0), InDemandedBits,
509  LHSKnownZero, LHSKnownOne, Depth+1))
510  return I;
511 
512  // If the RHS of the add has bits set that can't affect the input, reduce
513  // the constant.
514  if (ShrinkDemandedConstant(I, 1, InDemandedBits))
515  return I;
516 
517  // Avoid excess work.
518  if (LHSKnownZero == 0 && LHSKnownOne == 0)
519  break;
520 
521  // Turn it into OR if input bits are zero.
522  if ((LHSKnownZero & RHS->getValue()) == RHS->getValue()) {
523  Instruction *Or =
524  BinaryOperator::CreateOr(I->getOperand(0), I->getOperand(1),
525  I->getName());
526  return InsertNewInstWith(Or, *I);
527  }
528 
529  // We can say something about the output known-zero and known-one bits,
530  // depending on potential carries from the input constant and the
531  // unknowns. For example if the LHS is known to have at most the 0x0F0F0
532  // bits set and the RHS constant is 0x01001, then we know we have a known
533  // one mask of 0x00001 and a known zero mask of 0xE0F0E.
534 
535  // To compute this, we first compute the potential carry bits. These are
536  // the bits which may be modified. I'm not aware of a better way to do
537  // this scan.
538  const APInt &RHSVal = RHS->getValue();
539  APInt CarryBits((~LHSKnownZero + RHSVal) ^ (~LHSKnownZero ^ RHSVal));
540 
541  // Now that we know which bits have carries, compute the known-1/0 sets.
542 
543  // Bits are known one if they are known zero in one operand and one in the
544  // other, and there is no input carry.
545  KnownOne = ((LHSKnownZero & RHSVal) |
546  (LHSKnownOne & ~RHSVal)) & ~CarryBits;
547 
548  // Bits are known zero if they are known zero in both operands and there
549  // is no input carry.
550  KnownZero = LHSKnownZero & ~RHSVal & ~CarryBits;
551  } else {
552  // If the high-bits of this ADD are not demanded, then it does not demand
553  // the high bits of its LHS or RHS.
554  if (DemandedMask[BitWidth-1] == 0) {
555  // Right fill the mask of bits for this ADD to demand the most
556  // significant bit and all those below it.
557  APInt DemandedFromOps(APInt::getLowBitsSet(BitWidth, BitWidth-NLZ));
558  if (SimplifyDemandedBits(I->getOperandUse(0), DemandedFromOps,
559  LHSKnownZero, LHSKnownOne, Depth+1) ||
560  SimplifyDemandedBits(I->getOperandUse(1), DemandedFromOps,
561  LHSKnownZero, LHSKnownOne, Depth+1))
562  return I;
563  }
564  }
565  break;
566  }
567  case Instruction::Sub:
568  // If the high-bits of this SUB are not demanded, then it does not demand
569  // the high bits of its LHS or RHS.
570  if (DemandedMask[BitWidth-1] == 0) {
571  // Right fill the mask of bits for this SUB to demand the most
572  // significant bit and all those below it.
573  uint32_t NLZ = DemandedMask.countLeadingZeros();
574  APInt DemandedFromOps(APInt::getLowBitsSet(BitWidth, BitWidth-NLZ));
575  if (SimplifyDemandedBits(I->getOperandUse(0), DemandedFromOps,
576  LHSKnownZero, LHSKnownOne, Depth+1) ||
577  SimplifyDemandedBits(I->getOperandUse(1), DemandedFromOps,
578  LHSKnownZero, LHSKnownOne, Depth+1))
579  return I;
580  }
581 
582  // Otherwise just hand the sub off to computeKnownBits to fill in
583  // the known zeros and ones.
584  computeKnownBits(V, KnownZero, KnownOne, Depth);
585 
586  // Turn this into a xor if LHS is 2^n-1 and the remaining bits are known
587  // zero.
588  if (ConstantInt *C0 = dyn_cast<ConstantInt>(I->getOperand(0))) {
589  APInt I0 = C0->getValue();
590  if ((I0 + 1).isPowerOf2() && (I0 | KnownZero).isAllOnesValue()) {
591  Instruction *Xor = BinaryOperator::CreateXor(I->getOperand(1), C0);
592  return InsertNewInstWith(Xor, *I);
593  }
594  }
595  break;
596  case Instruction::Shl:
597  if (ConstantInt *SA = dyn_cast<ConstantInt>(I->getOperand(1))) {
598  {
599  Value *VarX; ConstantInt *C1;
600  if (match(I->getOperand(0), m_Shr(m_Value(VarX), m_ConstantInt(C1)))) {
601  Instruction *Shr = cast<Instruction>(I->getOperand(0));
602  Value *R = SimplifyShrShlDemandedBits(Shr, I, DemandedMask,
603  KnownZero, KnownOne);
604  if (R)
605  return R;
606  }
607  }
608 
609  uint64_t ShiftAmt = SA->getLimitedValue(BitWidth-1);
610  APInt DemandedMaskIn(DemandedMask.lshr(ShiftAmt));
611 
612  // If the shift is NUW/NSW, then it does demand the high bits.
613  ShlOperator *IOp = cast<ShlOperator>(I);
614  if (IOp->hasNoSignedWrap())
615  DemandedMaskIn |= APInt::getHighBitsSet(BitWidth, ShiftAmt+1);
616  else if (IOp->hasNoUnsignedWrap())
617  DemandedMaskIn |= APInt::getHighBitsSet(BitWidth, ShiftAmt);
618 
619  if (SimplifyDemandedBits(I->getOperandUse(0), DemandedMaskIn,
620  KnownZero, KnownOne, Depth+1))
621  return I;
622  assert(!(KnownZero & KnownOne) && "Bits known to be one AND zero?");
623  KnownZero <<= ShiftAmt;
624  KnownOne <<= ShiftAmt;
625  // low bits known zero.
626  if (ShiftAmt)
627  KnownZero |= APInt::getLowBitsSet(BitWidth, ShiftAmt);
628  }
629  break;
630  case Instruction::LShr:
631  // For a logical shift right
632  if (ConstantInt *SA = dyn_cast<ConstantInt>(I->getOperand(1))) {
633  uint64_t ShiftAmt = SA->getLimitedValue(BitWidth-1);
634 
635  // Unsigned shift right.
636  APInt DemandedMaskIn(DemandedMask.shl(ShiftAmt));
637 
638  // If the shift is exact, then it does demand the low bits (and knows that
639  // they are zero).
640  if (cast<LShrOperator>(I)->isExact())
641  DemandedMaskIn |= APInt::getLowBitsSet(BitWidth, ShiftAmt);
642 
643  if (SimplifyDemandedBits(I->getOperandUse(0), DemandedMaskIn,
644  KnownZero, KnownOne, Depth+1))
645  return I;
646  assert(!(KnownZero & KnownOne) && "Bits known to be one AND zero?");
647  KnownZero = APIntOps::lshr(KnownZero, ShiftAmt);
648  KnownOne = APIntOps::lshr(KnownOne, ShiftAmt);
649  if (ShiftAmt) {
650  // Compute the new bits that are at the top now.
651  APInt HighBits(APInt::getHighBitsSet(BitWidth, ShiftAmt));
652  KnownZero |= HighBits; // high bits known zero.
653  }
654  }
655  break;
656  case Instruction::AShr:
657  // If this is an arithmetic shift right and only the low-bit is set, we can
658  // always convert this into a logical shr, even if the shift amount is
659  // variable. The low bit of the shift cannot be an input sign bit unless
660  // the shift amount is >= the size of the datatype, which is undefined.
661  if (DemandedMask == 1) {
662  // Perform the logical shift right.
663  Instruction *NewVal = BinaryOperator::CreateLShr(
664  I->getOperand(0), I->getOperand(1), I->getName());
665  return InsertNewInstWith(NewVal, *I);
666  }
667 
668  // If the sign bit is the only bit demanded by this ashr, then there is no
669  // need to do it, the shift doesn't change the high bit.
670  if (DemandedMask.isSignBit())
671  return I->getOperand(0);
672 
673  if (ConstantInt *SA = dyn_cast<ConstantInt>(I->getOperand(1))) {
674  uint32_t ShiftAmt = SA->getLimitedValue(BitWidth-1);
675 
676  // Signed shift right.
677  APInt DemandedMaskIn(DemandedMask.shl(ShiftAmt));
678  // If any of the "high bits" are demanded, we should set the sign bit as
679  // demanded.
680  if (DemandedMask.countLeadingZeros() <= ShiftAmt)
681  DemandedMaskIn.setBit(BitWidth-1);
682 
683  // If the shift is exact, then it does demand the low bits (and knows that
684  // they are zero).
685  if (cast<AShrOperator>(I)->isExact())
686  DemandedMaskIn |= APInt::getLowBitsSet(BitWidth, ShiftAmt);
687 
688  if (SimplifyDemandedBits(I->getOperandUse(0), DemandedMaskIn,
689  KnownZero, KnownOne, Depth+1))
690  return I;
691  assert(!(KnownZero & KnownOne) && "Bits known to be one AND zero?");
692  // Compute the new bits that are at the top now.
693  APInt HighBits(APInt::getHighBitsSet(BitWidth, ShiftAmt));
694  KnownZero = APIntOps::lshr(KnownZero, ShiftAmt);
695  KnownOne = APIntOps::lshr(KnownOne, ShiftAmt);
696 
697  // Handle the sign bits.
698  APInt SignBit(APInt::getSignBit(BitWidth));
699  // Adjust to where it is now in the mask.
700  SignBit = APIntOps::lshr(SignBit, ShiftAmt);
701 
702  // If the input sign bit is known to be zero, or if none of the top bits
703  // are demanded, turn this into an unsigned shift right.
704  if (BitWidth <= ShiftAmt || KnownZero[BitWidth-ShiftAmt-1] ||
705  (HighBits & ~DemandedMask) == HighBits) {
706  // Perform the logical shift right.
707  BinaryOperator *NewVal = BinaryOperator::CreateLShr(I->getOperand(0),
708  SA, I->getName());
709  NewVal->setIsExact(cast<BinaryOperator>(I)->isExact());
710  return InsertNewInstWith(NewVal, *I);
711  } else if ((KnownOne & SignBit) != 0) { // New bits are known one.
712  KnownOne |= HighBits;
713  }
714  }
715  break;
716  case Instruction::SRem:
717  if (ConstantInt *Rem = dyn_cast<ConstantInt>(I->getOperand(1))) {
718  // X % -1 demands all the bits because we don't want to introduce
719  // INT_MIN % -1 (== undef) by accident.
720  if (Rem->isAllOnesValue())
721  break;
722  APInt RA = Rem->getValue().abs();
723  if (RA.isPowerOf2()) {
724  if (DemandedMask.ult(RA)) // srem won't affect demanded bits
725  return I->getOperand(0);
726 
727  APInt LowBits = RA - 1;
728  APInt Mask2 = LowBits | APInt::getSignBit(BitWidth);
729  if (SimplifyDemandedBits(I->getOperandUse(0), Mask2,
730  LHSKnownZero, LHSKnownOne, Depth+1))
731  return I;
732 
733  // The low bits of LHS are unchanged by the srem.
734  KnownZero = LHSKnownZero & LowBits;
735  KnownOne = LHSKnownOne & LowBits;
736 
737  // If LHS is non-negative or has all low bits zero, then the upper bits
738  // are all zero.
739  if (LHSKnownZero[BitWidth-1] || ((LHSKnownZero & LowBits) == LowBits))
740  KnownZero |= ~LowBits;
741 
742  // If LHS is negative and not all low bits are zero, then the upper bits
743  // are all one.
744  if (LHSKnownOne[BitWidth-1] && ((LHSKnownOne & LowBits) != 0))
745  KnownOne |= ~LowBits;
746 
747  assert(!(KnownZero & KnownOne) && "Bits known to be one AND zero?");
748  }
749  }
750 
751  // The sign bit is the LHS's sign bit, except when the result of the
752  // remainder is zero.
753  if (DemandedMask.isNegative() && KnownZero.isNonNegative()) {
754  APInt LHSKnownZero(BitWidth, 0), LHSKnownOne(BitWidth, 0);
755  computeKnownBits(I->getOperand(0), LHSKnownZero, LHSKnownOne, Depth+1);
756  // If it's known zero, our sign bit is also zero.
757  if (LHSKnownZero.isNegative())
758  KnownZero.setBit(KnownZero.getBitWidth() - 1);
759  }
760  break;
761  case Instruction::URem: {
762  APInt KnownZero2(BitWidth, 0), KnownOne2(BitWidth, 0);
763  APInt AllOnes = APInt::getAllOnesValue(BitWidth);
764  if (SimplifyDemandedBits(I->getOperandUse(0), AllOnes,
765  KnownZero2, KnownOne2, Depth+1) ||
766  SimplifyDemandedBits(I->getOperandUse(1), AllOnes,
767  KnownZero2, KnownOne2, Depth+1))
768  return I;
769 
770  unsigned Leaders = KnownZero2.countLeadingOnes();
771  Leaders = std::max(Leaders,
772  KnownZero2.countLeadingOnes());
773  KnownZero = APInt::getHighBitsSet(BitWidth, Leaders) & DemandedMask;
774  break;
775  }
776  case Instruction::Call:
777  if (IntrinsicInst *II = dyn_cast<IntrinsicInst>(I)) {
778  switch (II->getIntrinsicID()) {
779  default: break;
780  case Intrinsic::bswap: {
781  // If the only bits demanded come from one byte of the bswap result,
782  // just shift the input byte into position to eliminate the bswap.
783  unsigned NLZ = DemandedMask.countLeadingZeros();
784  unsigned NTZ = DemandedMask.countTrailingZeros();
785 
786  // Round NTZ down to the next byte. If we have 11 trailing zeros, then
787  // we need all the bits down to bit 8. Likewise, round NLZ. If we
788  // have 14 leading zeros, round to 8.
789  NLZ &= ~7;
790  NTZ &= ~7;
791  // If we need exactly one byte, we can do this transformation.
792  if (BitWidth-NLZ-NTZ == 8) {
793  unsigned ResultBit = NTZ;
794  unsigned InputBit = BitWidth-NTZ-8;
795 
796  // Replace this with either a left or right shift to get the byte into
797  // the right place.
798  Instruction *NewVal;
799  if (InputBit > ResultBit)
800  NewVal = BinaryOperator::CreateLShr(II->getArgOperand(0),
801  ConstantInt::get(I->getType(), InputBit-ResultBit));
802  else
803  NewVal = BinaryOperator::CreateShl(II->getArgOperand(0),
804  ConstantInt::get(I->getType(), ResultBit-InputBit));
805  NewVal->takeName(I);
806  return InsertNewInstWith(NewVal, *I);
807  }
808 
809  // TODO: Could compute known zero/one bits based on the input.
810  break;
811  }
812  case Intrinsic::x86_sse42_crc32_64_64:
813  KnownZero = APInt::getHighBitsSet(64, 32);
814  return nullptr;
815  }
816  }
817  computeKnownBits(V, KnownZero, KnownOne, Depth);
818  break;
819  }
820 
821  // If the client is only demanding bits that we know, return the known
822  // constant.
823  if ((DemandedMask & (KnownZero|KnownOne)) == DemandedMask)
824  return Constant::getIntegerValue(VTy, KnownOne);
825  return nullptr;
826 }
void clearAllBits()
Set every bit to 0.
Definition: APInt.h:1214
const Use & getOperandUse(unsigned i) const
Definition: User.h:101
Instruction * InsertNewInstWith(Instruction *New, Instruction &Old)
Definition: InstCombine.h:271
class_match< Value > m_Value()
m_Value() - Match an arbitrary value and ignore it.
Definition: PatternMatch.h:70
static APInt getSignBit(unsigned BitWidth)
Get the SignBit for a specific bit width.
Definition: APInt.h:441
APInt LLVM_ATTRIBUTE_UNUSED_RESULT abs() const
Get the absolute value;.
Definition: APInt.h:1546
static APInt getAllOnesValue(unsigned numBits)
Get the all-ones value.
Definition: APInt.h:448
void setBit(unsigned bitPosition)
Set a given bit to 1.
Definition: APInt.cpp:584
This class represents zero extension of integer types.
bool SimplifyDemandedBits(Use &U, APInt DemandedMask, APInt &KnownZero, APInt &KnownOne, unsigned Depth=0)
static APInt getLowBitsSet(unsigned numBits, unsigned loBitsSet)
Get a value with low bits set.
Definition: APInt.h:526
static bool ShrinkDemandedConstant(Instruction *I, unsigned OpNo, APInt Demanded)
static Constant * getNullValue(Type *Ty)
Definition: Constants.cpp:133
StringRef getName() const
Definition: Value.cpp:168
bool match(Val *V, const Pattern &P)
Definition: PatternMatch.h:42
Base class of casting instructions.
Definition: InstrTypes.h:387
const APInt & getValue() const
Return the constant's value.
Definition: Constants.h:105
BinOp2_match< LHS, RHS, Instruction::LShr, Instruction::AShr > m_Shr(const LHS &L, const RHS &R)
m_Shr - Matches LShr or AShr.
Definition: PatternMatch.h:618
APInt lshr(const APInt &LHS, unsigned shiftAmt)
Logical right-shift function.
Definition: APInt.h:1815
class_match< ConstantInt > m_ConstantInt()
m_ConstantInt() - Match an arbitrary ConstantInt and ignore it.
Definition: PatternMatch.h:72
void takeName(Value *V)
Definition: Value.cpp:242
static APInt getHighBitsSet(unsigned numBits, unsigned hiBitsSet)
Get a value with high bits set.
Definition: APInt.h:508
bool isIntOrIntVectorTy() const
Definition: Type.h:201
assert(Globals.size() > 1)
APInt LLVM_ATTRIBUTE_UNUSED_RESULT trunc(unsigned width) const
Truncate to new width.
Definition: APInt.cpp:920
bool isVectorTy() const
Definition: Type.h:226
LLVM Constant Representation.
Definition: Constant.h:41
APInt Or(const APInt &LHS, const APInt &RHS)
Bitwise OR function for APInt.
Definition: APInt.h:1870
APInt Xor(const APInt &LHS, const APInt &RHS)
Bitwise XOR function for APInt.
Definition: APInt.h:1875
unsigned getBitWidth() const
Return the number of bits in the APInt.
Definition: APInt.h:1248
Value * getOperand(unsigned i) const
Definition: User.h:90
bool isPointerTy() const
Definition: Type.h:217
static UndefValue * get(Type *T)
Definition: Constants.cpp:1372
bool isPowerOf2() const
Check if this APInt's value is a power of two greater than zero.
Definition: APInt.h:388
static Constant * getIntegerValue(Type *Ty, const APInt &V)
Definition: Constants.cpp:168
void setIsExact(bool b=true)
Class for constant integers.
Definition: Constants.h:51
unsigned getScalarSizeInBits() const LLVM_READONLY
Definition: Type.cpp:135
Type * getType() const
Definition: Value.h:215
static Constant * get(Type *Ty, uint64_t V, bool isSigned=false)
Definition: Constants.cpp:528
Class for arbitrary precision integers.
Definition: APInt.h:75
LLVM_ATTRIBUTE_UNUSED_RESULT std::enable_if< !is_simple_type< Y >::value, typename cast_retty< X, const Y >::ret_type >::type dyn_cast(const Y &Val)
Definition: Casting.h:265
const Type * getScalarType() const LLVM_READONLY
Definition: Type.cpp:51
APInt And(const APInt &LHS, const APInt &RHS)
Bitwise AND function for APInt.
Definition: APInt.h:1865
#define I(x, y, z)
Definition: MD5.cpp:54
bool hasOneUse() const
Definition: Value.h:284
const DataLayout * DL
Definition: InstCombine.h:89
LLVM Value Representation.
Definition: Value.h:69
unsigned getOpcode() const
getOpcode() returns a member of one of the enums like Instruction::Add.
Definition: Instruction.h:86
uint64_t getTypeSizeInBits(Type *Ty) const
Definition: DataLayout.h:522
APInt LLVM_ATTRIBUTE_UNUSED_RESULT zext(unsigned width) const
Zero extend to a new width.
Definition: APInt.cpp:984
void computeKnownBits(Value *V, APInt &KnownZero, APInt &KnownOne, unsigned Depth=0) const
Definition: InstCombine.h:318
Value * SimplifyShrShlDemandedBits(Instruction *Lsr, Instruction *Sftl, APInt DemandedMask, APInt &KnownZero, APInt &KnownOne)
Value * InstCombiner::SimplifyDemandedVectorElts ( Value V,
APInt  DemandedElts,
APInt UndefElts,
unsigned  Depth = 0 
)
private

SimplifyDemandedVectorElts - The specified value produces a vector with any number of elements. DemandedElts contains the set of elements that are actually used by the caller. This method analyzes which elements of the operand are undef and returns that information in UndefElts.

If the information about demanded elements can be used to simplify the operation, the operation is simplified, then the resultant value is returned. This returns null if no change was made.

Definition at line 917 of file InstCombineSimplifyDemanded.cpp.

919  {
920  unsigned VWidth = cast<VectorType>(V->getType())->getNumElements();
921  APInt EltMask(APInt::getAllOnesValue(VWidth));
922  assert((DemandedElts & ~EltMask) == 0 && "Invalid DemandedElts!");
923 
924  if (isa<UndefValue>(V)) {
925  // If the entire vector is undefined, just return this info.
926  UndefElts = EltMask;
927  return nullptr;
928  }
929 
930  if (DemandedElts == 0) { // If nothing is demanded, provide undef.
931  UndefElts = EltMask;
932  return UndefValue::get(V->getType());
933  }
934 
935  UndefElts = 0;
936 
937  // Handle ConstantAggregateZero, ConstantVector, ConstantDataSequential.
938  if (Constant *C = dyn_cast<Constant>(V)) {
939  // Check if this is identity. If so, return 0 since we are not simplifying
940  // anything.
941  if (DemandedElts.isAllOnesValue())
942  return nullptr;
943 
944  Type *EltTy = cast<VectorType>(V->getType())->getElementType();
945  Constant *Undef = UndefValue::get(EltTy);
946 
948  for (unsigned i = 0; i != VWidth; ++i) {
949  if (!DemandedElts[i]) { // If not demanded, set to undef.
950  Elts.push_back(Undef);
951  UndefElts.setBit(i);
952  continue;
953  }
954 
955  Constant *Elt = C->getAggregateElement(i);
956  if (!Elt) return nullptr;
957 
958  if (isa<UndefValue>(Elt)) { // Already undef.
959  Elts.push_back(Undef);
960  UndefElts.setBit(i);
961  } else { // Otherwise, defined.
962  Elts.push_back(Elt);
963  }
964  }
965 
966  // If we changed the constant, return it.
967  Constant *NewCV = ConstantVector::get(Elts);
968  return NewCV != C ? NewCV : nullptr;
969  }
970 
971  // Limit search depth.
972  if (Depth == 10)
973  return nullptr;
974 
975  // If multiple users are using the root value, proceed with
976  // simplification conservatively assuming that all elements
977  // are needed.
978  if (!V->hasOneUse()) {
979  // Quit if we find multiple users of a non-root value though.
980  // They'll be handled when it's their turn to be visited by
981  // the main instcombine process.
982  if (Depth != 0)
983  // TODO: Just compute the UndefElts information recursively.
984  return nullptr;
985 
986  // Conservatively assume that all elements are needed.
987  DemandedElts = EltMask;
988  }
989 
991  if (!I) return nullptr; // Only analyze instructions.
992 
993  bool MadeChange = false;
994  APInt UndefElts2(VWidth, 0);
995  Value *TmpV;
996  switch (I->getOpcode()) {
997  default: break;
998 
999  case Instruction::InsertElement: {
1000  // If this is a variable index, we don't know which element it overwrites.
1001  // demand exactly the same input as we produce.
1002  ConstantInt *Idx = dyn_cast<ConstantInt>(I->getOperand(2));
1003  if (!Idx) {
1004  // Note that we can't propagate undef elt info, because we don't know
1005  // which elt is getting updated.
1006  TmpV = SimplifyDemandedVectorElts(I->getOperand(0), DemandedElts,
1007  UndefElts2, Depth+1);
1008  if (TmpV) { I->setOperand(0, TmpV); MadeChange = true; }
1009  break;
1010  }
1011 
1012  // If this is inserting an element that isn't demanded, remove this
1013  // insertelement.
1014  unsigned IdxNo = Idx->getZExtValue();
1015  if (IdxNo >= VWidth || !DemandedElts[IdxNo]) {
1016  Worklist.Add(I);
1017  return I->getOperand(0);
1018  }
1019 
1020  // Otherwise, the element inserted overwrites whatever was there, so the
1021  // input demanded set is simpler than the output set.
1022  APInt DemandedElts2 = DemandedElts;
1023  DemandedElts2.clearBit(IdxNo);
1024  TmpV = SimplifyDemandedVectorElts(I->getOperand(0), DemandedElts2,
1025  UndefElts, Depth+1);
1026  if (TmpV) { I->setOperand(0, TmpV); MadeChange = true; }
1027 
1028  // The inserted element is defined.
1029  UndefElts.clearBit(IdxNo);
1030  break;
1031  }
1032  case Instruction::ShuffleVector: {
1033  ShuffleVectorInst *Shuffle = cast<ShuffleVectorInst>(I);
1034  uint64_t LHSVWidth =
1035  cast<VectorType>(Shuffle->getOperand(0)->getType())->getNumElements();
1036  APInt LeftDemanded(LHSVWidth, 0), RightDemanded(LHSVWidth, 0);
1037  for (unsigned i = 0; i < VWidth; i++) {
1038  if (DemandedElts[i]) {
1039  unsigned MaskVal = Shuffle->getMaskValue(i);
1040  if (MaskVal != -1u) {
1041  assert(MaskVal < LHSVWidth * 2 &&
1042  "shufflevector mask index out of range!");
1043  if (MaskVal < LHSVWidth)
1044  LeftDemanded.setBit(MaskVal);
1045  else
1046  RightDemanded.setBit(MaskVal - LHSVWidth);
1047  }
1048  }
1049  }
1050 
1051  APInt UndefElts4(LHSVWidth, 0);
1052  TmpV = SimplifyDemandedVectorElts(I->getOperand(0), LeftDemanded,
1053  UndefElts4, Depth+1);
1054  if (TmpV) { I->setOperand(0, TmpV); MadeChange = true; }
1055 
1056  APInt UndefElts3(LHSVWidth, 0);
1057  TmpV = SimplifyDemandedVectorElts(I->getOperand(1), RightDemanded,
1058  UndefElts3, Depth+1);
1059  if (TmpV) { I->setOperand(1, TmpV); MadeChange = true; }
1060 
1061  bool NewUndefElts = false;
1062  for (unsigned i = 0; i < VWidth; i++) {
1063  unsigned MaskVal = Shuffle->getMaskValue(i);
1064  if (MaskVal == -1u) {
1065  UndefElts.setBit(i);
1066  } else if (!DemandedElts[i]) {
1067  NewUndefElts = true;
1068  UndefElts.setBit(i);
1069  } else if (MaskVal < LHSVWidth) {
1070  if (UndefElts4[MaskVal]) {
1071  NewUndefElts = true;
1072  UndefElts.setBit(i);
1073  }
1074  } else {
1075  if (UndefElts3[MaskVal - LHSVWidth]) {
1076  NewUndefElts = true;
1077  UndefElts.setBit(i);
1078  }
1079  }
1080  }
1081 
1082  if (NewUndefElts) {
1083  // Add additional discovered undefs.
1085  for (unsigned i = 0; i < VWidth; ++i) {
1086  if (UndefElts[i])
1088  else
1090  Shuffle->getMaskValue(i)));
1091  }
1092  I->setOperand(2, ConstantVector::get(Elts));
1093  MadeChange = true;
1094  }
1095  break;
1096  }
1097  case Instruction::Select: {
1098  APInt LeftDemanded(DemandedElts), RightDemanded(DemandedElts);
1099  if (ConstantVector* CV = dyn_cast<ConstantVector>(I->getOperand(0))) {
1100  for (unsigned i = 0; i < VWidth; i++) {
1101  if (CV->getAggregateElement(i)->isNullValue())
1102  LeftDemanded.clearBit(i);
1103  else
1104  RightDemanded.clearBit(i);
1105  }
1106  }
1107 
1108  TmpV = SimplifyDemandedVectorElts(I->getOperand(1), LeftDemanded,
1109  UndefElts, Depth+1);
1110  if (TmpV) { I->setOperand(1, TmpV); MadeChange = true; }
1111 
1112  TmpV = SimplifyDemandedVectorElts(I->getOperand(2), RightDemanded,
1113  UndefElts2, Depth+1);
1114  if (TmpV) { I->setOperand(2, TmpV); MadeChange = true; }
1115 
1116  // Output elements are undefined if both are undefined.
1117  UndefElts &= UndefElts2;
1118  break;
1119  }
1120  case Instruction::BitCast: {
1121  // Vector->vector casts only.
1122  VectorType *VTy = dyn_cast<VectorType>(I->getOperand(0)->getType());
1123  if (!VTy) break;
1124  unsigned InVWidth = VTy->getNumElements();
1125  APInt InputDemandedElts(InVWidth, 0);
1126  unsigned Ratio;
1127 
1128  if (VWidth == InVWidth) {
1129  // If we are converting from <4 x i32> -> <4 x f32>, we demand the same
1130  // elements as are demanded of us.
1131  Ratio = 1;
1132  InputDemandedElts = DemandedElts;
1133  } else if (VWidth > InVWidth) {
1134  // Untested so far.
1135  break;
1136 
1137  // If there are more elements in the result than there are in the source,
1138  // then an input element is live if any of the corresponding output
1139  // elements are live.
1140  Ratio = VWidth/InVWidth;
1141  for (unsigned OutIdx = 0; OutIdx != VWidth; ++OutIdx) {
1142  if (DemandedElts[OutIdx])
1143  InputDemandedElts.setBit(OutIdx/Ratio);
1144  }
1145  } else {
1146  // Untested so far.
1147  break;
1148 
1149  // If there are more elements in the source than there are in the result,
1150  // then an input element is live if the corresponding output element is
1151  // live.
1152  Ratio = InVWidth/VWidth;
1153  for (unsigned InIdx = 0; InIdx != InVWidth; ++InIdx)
1154  if (DemandedElts[InIdx/Ratio])
1155  InputDemandedElts.setBit(InIdx);
1156  }
1157 
1158  // div/rem demand all inputs, because they don't want divide by zero.
1159  TmpV = SimplifyDemandedVectorElts(I->getOperand(0), InputDemandedElts,
1160  UndefElts2, Depth+1);
1161  if (TmpV) {
1162  I->setOperand(0, TmpV);
1163  MadeChange = true;
1164  }
1165 
1166  UndefElts = UndefElts2;
1167  if (VWidth > InVWidth) {
1168  llvm_unreachable("Unimp");
1169  // If there are more elements in the result than there are in the source,
1170  // then an output element is undef if the corresponding input element is
1171  // undef.
1172  for (unsigned OutIdx = 0; OutIdx != VWidth; ++OutIdx)
1173  if (UndefElts2[OutIdx/Ratio])
1174  UndefElts.setBit(OutIdx);
1175  } else if (VWidth < InVWidth) {
1176  llvm_unreachable("Unimp");
1177  // If there are more elements in the source than there are in the result,
1178  // then a result element is undef if all of the corresponding input
1179  // elements are undef.
1180  UndefElts = ~0ULL >> (64-VWidth); // Start out all undef.
1181  for (unsigned InIdx = 0; InIdx != InVWidth; ++InIdx)
1182  if (!UndefElts2[InIdx]) // Not undef?
1183  UndefElts.clearBit(InIdx/Ratio); // Clear undef bit.
1184  }
1185  break;
1186  }
1187  case Instruction::And:
1188  case Instruction::Or:
1189  case Instruction::Xor:
1190  case Instruction::Add:
1191  case Instruction::Sub:
1192  case Instruction::Mul:
1193  // div/rem demand all inputs, because they don't want divide by zero.
1194  TmpV = SimplifyDemandedVectorElts(I->getOperand(0), DemandedElts,
1195  UndefElts, Depth+1);
1196  if (TmpV) { I->setOperand(0, TmpV); MadeChange = true; }
1197  TmpV = SimplifyDemandedVectorElts(I->getOperand(1), DemandedElts,
1198  UndefElts2, Depth+1);
1199  if (TmpV) { I->setOperand(1, TmpV); MadeChange = true; }
1200 
1201  // Output elements are undefined if both are undefined. Consider things
1202  // like undef&0. The result is known zero, not undef.
1203  UndefElts &= UndefElts2;
1204  break;
1205  case Instruction::FPTrunc:
1206  case Instruction::FPExt:
1207  TmpV = SimplifyDemandedVectorElts(I->getOperand(0), DemandedElts,
1208  UndefElts, Depth+1);
1209  if (TmpV) { I->setOperand(0, TmpV); MadeChange = true; }
1210  break;
1211 
1212  case Instruction::Call: {
1214  if (!II) break;
1215  switch (II->getIntrinsicID()) {
1216  default: break;
1217 
1218  // Binary vector operations that work column-wise. A dest element is a
1219  // function of the corresponding input elements from the two inputs.
1220  case Intrinsic::x86_sse_sub_ss:
1221  case Intrinsic::x86_sse_mul_ss:
1222  case Intrinsic::x86_sse_min_ss:
1223  case Intrinsic::x86_sse_max_ss:
1224  case Intrinsic::x86_sse2_sub_sd:
1225  case Intrinsic::x86_sse2_mul_sd:
1226  case Intrinsic::x86_sse2_min_sd:
1227  case Intrinsic::x86_sse2_max_sd:
1228  TmpV = SimplifyDemandedVectorElts(II->getArgOperand(0), DemandedElts,
1229  UndefElts, Depth+1);
1230  if (TmpV) { II->setArgOperand(0, TmpV); MadeChange = true; }
1231  TmpV = SimplifyDemandedVectorElts(II->getArgOperand(1), DemandedElts,
1232  UndefElts2, Depth+1);
1233  if (TmpV) { II->setArgOperand(1, TmpV); MadeChange = true; }
1234 
1235  // If only the low elt is demanded and this is a scalarizable intrinsic,
1236  // scalarize it now.
1237  if (DemandedElts == 1) {
1238  switch (II->getIntrinsicID()) {
1239  default: break;
1240  case Intrinsic::x86_sse_sub_ss:
1241  case Intrinsic::x86_sse_mul_ss:
1242  case Intrinsic::x86_sse2_sub_sd:
1243  case Intrinsic::x86_sse2_mul_sd:
1244  // TODO: Lower MIN/MAX/ABS/etc
1245  Value *LHS = II->getArgOperand(0);
1246  Value *RHS = II->getArgOperand(1);
1247  // Extract the element as scalars.
1249  ConstantInt::get(Type::getInt32Ty(I->getContext()), 0U)), *II);
1251  ConstantInt::get(Type::getInt32Ty(I->getContext()), 0U)), *II);
1252 
1253  switch (II->getIntrinsicID()) {
1254  default: llvm_unreachable("Case stmts out of sync!");
1255  case Intrinsic::x86_sse_sub_ss:
1256  case Intrinsic::x86_sse2_sub_sd:
1257  TmpV = InsertNewInstWith(BinaryOperator::CreateFSub(LHS, RHS,
1258  II->getName()), *II);
1259  break;
1260  case Intrinsic::x86_sse_mul_ss:
1261  case Intrinsic::x86_sse2_mul_sd:
1262  TmpV = InsertNewInstWith(BinaryOperator::CreateFMul(LHS, RHS,
1263  II->getName()), *II);
1264  break;
1265  }
1266 
1267  Instruction *New =
1269  UndefValue::get(II->getType()), TmpV,
1270  ConstantInt::get(Type::getInt32Ty(I->getContext()), 0U, false),
1271  II->getName());
1272  InsertNewInstWith(New, *II);
1273  return New;
1274  }
1275  }
1276 
1277  // Output elements are undefined if both are undefined. Consider things
1278  // like undef&0. The result is known zero, not undef.
1279  UndefElts &= UndefElts2;
1280  break;
1281  }
1282  break;
1283  }
1284  }
1285  return MadeChange ? I : nullptr;
1286 }
static int getMaskValue(Constant *Mask, unsigned i)
Instruction * InsertNewInstWith(Instruction *New, Instruction &Old)
Definition: InstCombine.h:271
static APInt getAllOnesValue(unsigned numBits)
Get the all-ones value.
Definition: APInt.h:448
Intrinsic::ID getIntrinsicID() const
Definition: IntrinsicInst.h:43
void setBit(unsigned bitPosition)
Set a given bit to 1.
Definition: APInt.cpp:584
Value * SimplifyDemandedVectorElts(Value *V, APInt DemandedElts, APInt &UndefElts, unsigned Depth=0)
void Add(Instruction *I)
StringRef getName() const
Definition: Value.cpp:168
static InsertElementInst * Create(Value *Vec, Value *NewElt, Value *Idx, const Twine &NameStr="", Instruction *InsertBefore=nullptr)
#define llvm_unreachable(msg)
Definition: ErrorHandling.h:98
static Constant * get(ArrayRef< Constant * > V)
Definition: Constants.cpp:961
uint64_t getZExtValue() const
Return the zero extended value.
Definition: Constants.h:116
unsigned getNumElements() const
Return the number of elements in the Vector type.
Definition: DerivedTypes.h:428
assert(Globals.size() > 1)
LLVM Constant Representation.
Definition: Constant.h:41
APInt Or(const APInt &LHS, const APInt &RHS)
Bitwise OR function for APInt.
Definition: APInt.h:1870
APInt Xor(const APInt &LHS, const APInt &RHS)
Bitwise XOR function for APInt.
Definition: APInt.h:1875
Value * getOperand(unsigned i) const
Definition: User.h:90
InstCombineWorklist Worklist
Worklist - All of the instructions that need to be simplified.
Definition: InstCombine.h:97
static UndefValue * get(Type *T)
Definition: Constants.cpp:1372
LLVMContext & getContext() const
All values hold a context through their type.
Definition: Value.cpp:615
Class for constant integers.
Definition: Constants.h:51
Type * getType() const
Definition: Value.h:215
static Constant * get(Type *Ty, uint64_t V, bool isSigned=false)
Definition: Constants.cpp:528
void setOperand(unsigned i, Value *Val)
Definition: User.h:94
Value * getArgOperand(unsigned i) const
Class for arbitrary precision integers.
Definition: APInt.h:75
LLVM_ATTRIBUTE_UNUSED_RESULT std::enable_if< !is_simple_type< Y >::value, typename cast_retty< X, const Y >::ret_type >::type dyn_cast(const Y &Val)
Definition: Casting.h:265
APInt And(const APInt &LHS, const APInt &RHS)
Bitwise AND function for APInt.
Definition: APInt.h:1865
bool isAllOnesValue() const
Determine if all bits are set.
Definition: APInt.h:338
void Shuffle(internal::Random *random, std::vector< E > *v)
static IntegerType * getInt32Ty(LLVMContext &C)
Definition: Type.cpp:235
void clearBit(unsigned bitPosition)
Set a given bit to 0.
Definition: APInt.cpp:593
#define I(x, y, z)
Definition: MD5.cpp:54
bool hasOneUse() const
Definition: Value.h:284
void setArgOperand(unsigned i, Value *v)
LLVM Value Representation.
Definition: Value.h:69
unsigned getOpcode() const
getOpcode() returns a member of one of the enums like Instruction::Add.
Definition: Instruction.h:86
static ExtractElementInst * Create(Value *Vec, Value *Idx, const Twine &NameStr="", Instruction *InsertBefore=nullptr)
bool InstCombiner::SimplifyDivRemOfSelect ( BinaryOperator I)

SimplifyDivRemOfSelect - Try to fold a divide or remainder of a select instruction.

Definition at line 640 of file InstCombineMulDivRem.cpp.

640  {
641  SelectInst *SI = cast<SelectInst>(I.getOperand(1));
642 
643  // div/rem X, (Cond ? 0 : Y) -> div/rem X, Y
644  int NonNullOperand = -1;
645  if (Constant *ST = dyn_cast<Constant>(SI->getOperand(1)))
646  if (ST->isNullValue())
647  NonNullOperand = 2;
648  // div/rem X, (Cond ? Y : 0) -> div/rem X, Y
649  if (Constant *ST = dyn_cast<Constant>(SI->getOperand(2)))
650  if (ST->isNullValue())
651  NonNullOperand = 1;
652 
653  if (NonNullOperand == -1)
654  return false;
655 
656  Value *SelectCond = SI->getOperand(0);
657 
658  // Change the div/rem to use 'Y' instead of the select.
659  I.setOperand(1, SI->getOperand(NonNullOperand));
660 
661  // Okay, we know we replace the operand of the div/rem with 'Y' with no
662  // problem. However, the select, or the condition of the select may have
663  // multiple uses. Based on our knowledge that the operand must be non-zero,
664  // propagate the known value for the select into other uses of it, and
665  // propagate a known value of the condition into its other users.
666 
667  // If the select and condition only have a single use, don't bother with this,
668  // early exit.
669  if (SI->use_empty() && SelectCond->hasOneUse())
670  return true;
671 
672  // Scan the current block backward, looking for other uses of SI.
673  BasicBlock::iterator BBI = &I, BBFront = I.getParent()->begin();
674 
675  while (BBI != BBFront) {
676  --BBI;
677  // If we found a call to a function, we can't assume it will return, so
678  // information from below it cannot be propagated above it.
679  if (isa<CallInst>(BBI) && !isa<IntrinsicInst>(BBI))
680  break;
681 
682  // Replace uses of the select or its condition with the known values.
683  for (Instruction::op_iterator I = BBI->op_begin(), E = BBI->op_end();
684  I != E; ++I) {
685  if (*I == SI) {
686  *I = SI->getOperand(NonNullOperand);
687  Worklist.Add(BBI);
688  } else if (*I == SelectCond) {
689  *I = Builder->getInt1(NonNullOperand == 1);
690  Worklist.Add(BBI);
691  }
692  }
693 
694  // If we past the instruction, quit looking for it.
695  if (&*BBI == SI)
696  SI = nullptr;
697  if (&*BBI == SelectCond)
698  SelectCond = nullptr;
699 
700  // If we ran out of things to eliminate, break out of the loop.
701  if (!SelectCond && !SI)
702  break;
703 
704  }
705  return true;
706 }
void Add(Instruction *I)
iterator begin()
Definition: BasicBlock.h:198
ConstantInt * getInt1(bool V)
Get a constant value representing either true or false.
Definition: IRBuilder.h:251
LLVM Constant Representation.
Definition: Constant.h:41
Value * getOperand(unsigned i) const
Definition: User.h:90
InstCombineWorklist Worklist
Worklist - All of the instructions that need to be simplified.
Definition: InstCombine.h:97
BuilderTy * Builder
Definition: InstCombine.h:102
void setOperand(unsigned i, Value *Val)
Definition: User.h:94
#define I(x, y, z)
Definition: MD5.cpp:54
bool hasOneUse() const
Definition: Value.h:284
bool use_empty() const
Definition: Value.h:250
Use * op_iterator
Definition: User.h:115
LLVM Value Representation.
Definition: Value.h:69
const BasicBlock * getParent() const
Definition: Instruction.h:53
Instruction * InstCombiner::SimplifyMemSet ( MemSetInst MI)
private

Definition at line 156 of file InstCombineCalls.cpp.

156  {
157  unsigned Alignment = getKnownAlignment(MI->getDest(), DL);
158  if (MI->getAlignment() < Alignment) {
160  Alignment, false));
161  return MI;
162  }
163 
164  // Extract the length and alignment and fill if they are constant.
165  ConstantInt *LenC = dyn_cast<ConstantInt>(MI->getLength());
166  ConstantInt *FillC = dyn_cast<ConstantInt>(MI->getValue());
167  if (!LenC || !FillC || !FillC->getType()->isIntegerTy(8))
168  return nullptr;
169  uint64_t Len = LenC->getLimitedValue();
170  Alignment = MI->getAlignment();
171  assert(Len && "0-sized memory setting should be removed already.");
172 
173  // memset(s,c,n) -> store s, c (for n=1,2,4,8)
174  if (Len <= 8 && isPowerOf2_32((uint32_t)Len)) {
175  Type *ITy = IntegerType::get(MI->getContext(), Len*8); // n=1 -> i8.
176 
177  Value *Dest = MI->getDest();
178  unsigned DstAddrSp = cast<PointerType>(Dest->getType())->getAddressSpace();
179  Type *NewDstPtrTy = PointerType::get(ITy, DstAddrSp);
180  Dest = Builder->CreateBitCast(Dest, NewDstPtrTy);
181 
182  // Alignment 0 is identity for alignment 1 for memset, but not store.
183  if (Alignment == 0) Alignment = 1;
184 
185  // Extract the fill value and store.
186  uint64_t Fill = FillC->getZExtValue()*0x0101010101010101ULL;
187  StoreInst *S = Builder->CreateStore(ConstantInt::get(ITy, Fill), Dest,
188  MI->isVolatile());
189  S->setAlignment(Alignment);
190 
191  // Set the size of the copy to 0, it will be deleted on the next iteration.
193  return MI;
194  }
195 
196  return nullptr;
197 }
unsigned getAlignment() const
IntegerType * getType() const
Definition: Constants.h:139
bool isVolatile() const
Value * getValue() const
static PointerType * get(Type *ElementType, unsigned AddressSpace)
Definition: Type.cpp:727
void setAlignment(Constant *A)
static Constant * getNullValue(Type *Ty)
Definition: Constants.cpp:133
Type * getAlignmentType() const
uint64_t getLimitedValue(uint64_t Limit=~0ULL) const
Get the constant's value with a saturation limit.
Definition: Constants.h:218
StoreInst * CreateStore(Value *Val, Value *Ptr, bool isVolatile=false)
Definition: IRBuilder.h:910
assert(Globals.size() > 1)
BuilderTy * Builder
Definition: InstCombine.h:102
LLVMContext & getContext() const
All values hold a context through their type.
Definition: Value.cpp:615
static IntegerType * get(LLVMContext &C, unsigned NumBits)
Get or create an IntegerType instance.
Definition: Type.cpp:299
Class for constant integers.
Definition: Constants.h:51
Value * getDest() const
Value * CreateBitCast(Value *V, Type *DestTy, const Twine &Name="")
Definition: IRBuilder.h:1158
Value * getLength() const
static Constant * get(Type *Ty, uint64_t V, bool isSigned=false)
Definition: Constants.cpp:528
static unsigned getKnownAlignment(Value *V, const DataLayout *TD=nullptr)
getKnownAlignment - Try to infer an alignment for the specified pointer.
Definition: Local.h:176
bool isIntegerTy() const
Definition: Type.h:193
LLVM_ATTRIBUTE_UNUSED_RESULT std::enable_if< !is_simple_type< Y >::value, typename cast_retty< X, const Y >::ret_type >::type dyn_cast(const Y &Val)
Definition: Casting.h:265
void setLength(Value *L)
const DataLayout * DL
Definition: InstCombine.h:89
LLVM Value Representation.
Definition: Value.h:69
void setAlignment(unsigned Align)
bool isPowerOf2_32(uint32_t Value)
Definition: MathExtras.h:363
Instruction * InstCombiner::SimplifyMemTransfer ( MemIntrinsic MI)
private

Definition at line 60 of file InstCombineCalls.cpp.

60  {
61  unsigned DstAlign = getKnownAlignment(MI->getArgOperand(0), DL);
62  unsigned SrcAlign = getKnownAlignment(MI->getArgOperand(1), DL);
63  unsigned MinAlign = std::min(DstAlign, SrcAlign);
64  unsigned CopyAlign = MI->getAlignment();
65 
66  if (CopyAlign < MinAlign) {
68  MinAlign, false));
69  return MI;
70  }
71 
72  // If MemCpyInst length is 1/2/4/8 bytes then replace memcpy with
73  // load/store.
74  ConstantInt *MemOpLength = dyn_cast<ConstantInt>(MI->getArgOperand(2));
75  if (!MemOpLength) return nullptr;
76 
77  // Source and destination pointer types are always "i8*" for intrinsic. See
78  // if the size is something we can handle with a single primitive load/store.
79  // A single load+store correctly handles overlapping memory in the memmove
80  // case.
81  uint64_t Size = MemOpLength->getLimitedValue();
82  assert(Size && "0-sized memory transferring should be removed already.");
83 
84  if (Size > 8 || (Size&(Size-1)))
85  return nullptr; // If not 1/2/4/8 bytes, exit.
86 
87  // Use an integer load+store unless we can find something better.
88  unsigned SrcAddrSp =
89  cast<PointerType>(MI->getArgOperand(1)->getType())->getAddressSpace();
90  unsigned DstAddrSp =
91  cast<PointerType>(MI->getArgOperand(0)->getType())->getAddressSpace();
92 
93  IntegerType* IntType = IntegerType::get(MI->getContext(), Size<<3);
94  Type *NewSrcPtrTy = PointerType::get(IntType, SrcAddrSp);
95  Type *NewDstPtrTy = PointerType::get(IntType, DstAddrSp);
96 
97  // Memcpy forces the use of i8* for the source and destination. That means
98  // that if you're using memcpy to move one double around, you'll get a cast
99  // from double* to i8*. We'd much rather use a double load+store rather than
100  // an i64 load+store, here because this improves the odds that the source or
101  // dest address will be promotable. See if we can find a better type than the
102  // integer datatype.
103  Value *StrippedDest = MI->getArgOperand(0)->stripPointerCasts();
104  MDNode *CopyMD = nullptr;
105  if (StrippedDest != MI->getArgOperand(0)) {
106  Type *SrcETy = cast<PointerType>(StrippedDest->getType())
107  ->getElementType();
108  if (DL && SrcETy->isSized() && DL->getTypeStoreSize(SrcETy) == Size) {
109  // The SrcETy might be something like {{{double}}} or [1 x double]. Rip
110  // down through these levels if so.
111  SrcETy = reduceToSingleValueType(SrcETy);
112 
113  if (SrcETy->isSingleValueType()) {
114  NewSrcPtrTy = PointerType::get(SrcETy, SrcAddrSp);
115  NewDstPtrTy = PointerType::get(SrcETy, DstAddrSp);
116 
117  // If the memcpy has metadata describing the members, see if we can
118  // get the TBAA tag describing our copy.
120  if (M->getNumOperands() == 3 &&
121  M->getOperand(0) &&
122  isa<ConstantInt>(M->getOperand(0)) &&
123  cast<ConstantInt>(M->getOperand(0))->isNullValue() &&
124  M->getOperand(1) &&
125  isa<ConstantInt>(M->getOperand(1)) &&
126  cast<ConstantInt>(M->getOperand(1))->getValue() == Size &&
127  M->getOperand(2) &&
128  isa<MDNode>(M->getOperand(2)))
129  CopyMD = cast<MDNode>(M->getOperand(2));
130  }
131  }
132  }
133  }
134 
135  // If the memcpy/memmove provides better alignment info than we can
136  // infer, use it.
137  SrcAlign = std::max(SrcAlign, CopyAlign);
138  DstAlign = std::max(DstAlign, CopyAlign);
139 
140  Value *Src = Builder->CreateBitCast(MI->getArgOperand(1), NewSrcPtrTy);
141  Value *Dest = Builder->CreateBitCast(MI->getArgOperand(0), NewDstPtrTy);
142  LoadInst *L = Builder->CreateLoad(Src, MI->isVolatile());
143  L->setAlignment(SrcAlign);
144  if (CopyMD)
145  L->setMetadata(LLVMContext::MD_tbaa, CopyMD);
146  StoreInst *S = Builder->CreateStore(L, Dest, MI->isVolatile());
147  S->setAlignment(DstAlign);
148  if (CopyMD)
149  S->setMetadata(LLVMContext::MD_tbaa, CopyMD);
150 
151  // Set the size of the copy to 0, it will be deleted on the next iteration.
152  MI->setArgOperand(2, Constant::getNullValue(MemOpLength->getType()));
153  return MI;
154 }
unsigned getAlignment() const
IntegerType * getType() const
Definition: Constants.h:139
LoadInst * CreateLoad(Value *Ptr, const char *Name)
Definition: IRBuilder.h:901
bool isVolatile() const
static PointerType * get(Type *ElementType, unsigned AddressSpace)
Definition: Type.cpp:727
MDNode - a tuple of other values.
Definition: Metadata.h:74
void setAlignment(Constant *A)
static Constant * getNullValue(Type *Ty)
Definition: Constants.cpp:133
bool isSingleValueType() const
Definition: Type.h:250
Type * getAlignmentType() const
uint64_t getLimitedValue(uint64_t Limit=~0ULL) const
Get the constant's value with a saturation limit.
Definition: Constants.h:218
StoreInst * CreateStore(Value *Val, Value *Ptr, bool isVolatile=false)
Definition: IRBuilder.h:910
assert(Globals.size() > 1)
static Type * reduceToSingleValueType(Type *T)
Integer representation type.
Definition: DerivedTypes.h:37
void setAlignment(unsigned Align)
BuilderTy * Builder
Definition: InstCombine.h:102
LLVMContext & getContext() const
All values hold a context through their type.
Definition: Value.cpp:615
void setMetadata(unsigned KindID, MDNode *Node)
Definition: Metadata.cpp:634
static IntegerType * get(LLVMContext &C, unsigned NumBits)
Get or create an IntegerType instance.
Definition: Type.cpp:299
Class for constant integers.
Definition: Constants.h:51
Value * CreateBitCast(Value *V, Type *DestTy, const Twine &Name="")
Definition: IRBuilder.h:1158
Type * getType() const
Definition: Value.h:215
MDNode * getMetadata(unsigned KindID) const
Definition: Instruction.h:143
Value * stripPointerCasts()
Strips off any unneeded pointer casts, all-zero GEPs and aliases from the specified value...
Definition: Value.cpp:423
static Constant * get(Type *Ty, uint64_t V, bool isSigned=false)
Definition: Constants.cpp:528
Value * getArgOperand(unsigned i) const
bool isSized(SmallPtrSet< const Type *, 4 > *Visited=nullptr) const
Definition: Type.h:268
static unsigned getKnownAlignment(Value *V, const DataLayout *TD=nullptr)
getKnownAlignment - Try to infer an alignment for the specified pointer.
Definition: Local.h:176
LLVM_ATTRIBUTE_UNUSED_RESULT std::enable_if< !is_simple_type< Y >::value, typename cast_retty< X, const Y >::ret_type >::type dyn_cast(const Y &Val)
Definition: Casting.h:265
uint64_t MinAlign(uint64_t A, uint64_t B)
Definition: MathExtras.h:544
void setArgOperand(unsigned i, Value *v)
uint64_t getTypeStoreSize(Type *Ty) const
Definition: DataLayout.h:353
const DataLayout * DL
Definition: InstCombine.h:89
LLVM Value Representation.
Definition: Value.h:69
void setAlignment(unsigned Align)
Value * InstCombiner::SimplifyShrShlDemandedBits ( Instruction Shr,
Instruction Shl,
APInt  DemandedMask,
APInt KnownZero,
APInt KnownOne 
)
private

Helper routine of SimplifyDemandedUseBits. It tries to simplify demanded bit for "r1 = shr x, c1; r2 = shl r1, c2" instruction sequence.

Helper routine of SimplifyDemandedUseBits. It tries to simplify "E1 = (X lsr C1) << C2", where the C1 and C2 are constant, into "E2 = X << (C2 - C1)" or "E2 = X >> (C1 - C2)", depending on the sign of "C2-C1".

Suppose E1 and E2 are generally different in bits S={bm, bm+1, ..., bn}, without considering the specific value X is holding. This transformation is legal iff one of following conditions is hold: 1) All the bit in S are 0, in this case E1 == E2. 2) We don't care those bits in S, per the input DemandedMask. 3) Combination of 1) and 2). Some bits in S are 0, and we don't care the rest bits.

Currently we only test condition 2).

As with SimplifyDemandedUseBits, it returns NULL if the simplification was not successful.

Definition at line 845 of file InstCombineSimplifyDemanded.cpp.

846  {
847 
848  const APInt &ShlOp1 = cast<ConstantInt>(Shl->getOperand(1))->getValue();
849  const APInt &ShrOp1 = cast<ConstantInt>(Shr->getOperand(1))->getValue();
850  if (!ShlOp1 || !ShrOp1)
851  return nullptr; // Noop.
852 
853  Value *VarX = Shr->getOperand(0);
854  Type *Ty = VarX->getType();
855  unsigned BitWidth = Ty->getIntegerBitWidth();
856  if (ShlOp1.uge(BitWidth) || ShrOp1.uge(BitWidth))
857  return nullptr; // Undef.
858 
859  unsigned ShlAmt = ShlOp1.getZExtValue();
860  unsigned ShrAmt = ShrOp1.getZExtValue();
861 
862  KnownOne.clearAllBits();
863  KnownZero = APInt::getBitsSet(KnownZero.getBitWidth(), 0, ShlAmt-1);
864  KnownZero &= DemandedMask;
865 
866  APInt BitMask1(APInt::getAllOnesValue(BitWidth));
867  APInt BitMask2(APInt::getAllOnesValue(BitWidth));
868 
869  bool isLshr = (Shr->getOpcode() == Instruction::LShr);
870  BitMask1 = isLshr ? (BitMask1.lshr(ShrAmt) << ShlAmt) :
871  (BitMask1.ashr(ShrAmt) << ShlAmt);
872 
873  if (ShrAmt <= ShlAmt) {
874  BitMask2 <<= (ShlAmt - ShrAmt);
875  } else {
876  BitMask2 = isLshr ? BitMask2.lshr(ShrAmt - ShlAmt):
877  BitMask2.ashr(ShrAmt - ShlAmt);
878  }
879 
880  // Check if condition-2 (see the comment to this function) is satified.
881  if ((BitMask1 & DemandedMask) == (BitMask2 & DemandedMask)) {
882  if (ShrAmt == ShlAmt)
883  return VarX;
884 
885  if (!Shr->hasOneUse())
886  return nullptr;
887 
888  BinaryOperator *New;
889  if (ShrAmt < ShlAmt) {
890  Constant *Amt = ConstantInt::get(VarX->getType(), ShlAmt - ShrAmt);
891  New = BinaryOperator::CreateShl(VarX, Amt);
892  BinaryOperator *Orig = cast<BinaryOperator>(Shl);
893  New->setHasNoSignedWrap(Orig->hasNoSignedWrap());
894  New->setHasNoUnsignedWrap(Orig->hasNoUnsignedWrap());
895  } else {
896  Constant *Amt = ConstantInt::get(VarX->getType(), ShrAmt - ShlAmt);
897  New = isLshr ? BinaryOperator::CreateLShr(VarX, Amt) :
898  BinaryOperator::CreateAShr(VarX, Amt);
899  if (cast<BinaryOperator>(Shr)->isExact())
900  New->setIsExact(true);
901  }
902 
903  return InsertNewInstWith(New, *Shl);
904  }
905 
906  return nullptr;
907 }
void clearAllBits()
Set every bit to 0.
Definition: APInt.h:1214
Instruction * InsertNewInstWith(Instruction *New, Instruction &Old)
Definition: InstCombine.h:271
void setHasNoSignedWrap(bool b=true)
static APInt getAllOnesValue(unsigned numBits)
Get the all-ones value.
Definition: APInt.h:448
uint64_t getZExtValue() const
Get zero extended value.
Definition: APInt.h:1302
LLVM Constant Representation.
Definition: Constant.h:41
unsigned getBitWidth() const
Return the number of bits in the APInt.
Definition: APInt.h:1248
bool uge(const APInt &RHS) const
Unsigned greater or equal comparison.
Definition: APInt.h:1112
Value * getOperand(unsigned i) const
Definition: User.h:90
bool hasNoSignedWrap() const
hasNoSignedWrap - Determine whether the no signed wrap flag is set.
void setIsExact(bool b=true)
unsigned getIntegerBitWidth() const
Definition: Type.cpp:172
Type * getType() const
Definition: Value.h:215
static Constant * get(Type *Ty, uint64_t V, bool isSigned=false)
Definition: Constants.cpp:528
Class for arbitrary precision integers.
Definition: APInt.h:75
static APInt getBitsSet(unsigned numBits, unsigned loBit, unsigned hiBit)
Get a value with a block of bits set.
Definition: APInt.h:493
bool hasOneUse() const
Definition: Value.h:284
LLVM Value Representation.
Definition: Value.h:69
bool hasNoUnsignedWrap() const
hasNoUnsignedWrap - Determine whether the no unsigned wrap flag is set.
unsigned getOpcode() const
getOpcode() returns a member of one of the enums like Instruction::Add.
Definition: Instruction.h:86
bool InstCombiner::SimplifyStoreAtEndOfBlock ( StoreInst SI)
private

SimplifyStoreAtEndOfBlock - Turn things like: if () { *P = v1; } else { *P = v2 } into a phi node with a store in the successor.

Simplify things like: *P = v1; if () { *P = v2; } into a phi node with a store in the successor.

Definition at line 726 of file InstCombineLoadStoreAlloca.cpp.

726  {
727  BasicBlock *StoreBB = SI.getParent();
728 
729  // Check to see if the successor block has exactly two incoming edges. If
730  // so, see if the other predecessor contains a store to the same location.
731  // if so, insert a PHI node (if needed) and move the stores down.
732  BasicBlock *DestBB = StoreBB->getTerminator()->getSuccessor(0);
733 
734  // Determine whether Dest has exactly two predecessors and, if so, compute
735  // the other predecessor.
736  pred_iterator PI = pred_begin(DestBB);
737  BasicBlock *P = *PI;
738  BasicBlock *OtherBB = nullptr;
739 
740  if (P != StoreBB)
741  OtherBB = P;
742 
743  if (++PI == pred_end(DestBB))
744  return false;
745 
746  P = *PI;
747  if (P != StoreBB) {
748  if (OtherBB)
749  return false;
750  OtherBB = P;
751  }
752  if (++PI != pred_end(DestBB))
753  return false;
754 
755  // Bail out if all the relevant blocks aren't distinct (this can happen,
756  // for example, if SI is in an infinite loop)
757  if (StoreBB == DestBB || OtherBB == DestBB)
758  return false;
759 
760  // Verify that the other block ends in a branch and is not otherwise empty.
761  BasicBlock::iterator BBI = OtherBB->getTerminator();
762  BranchInst *OtherBr = dyn_cast<BranchInst>(BBI);
763  if (!OtherBr || BBI == OtherBB->begin())
764  return false;
765 
766  // If the other block ends in an unconditional branch, check for the 'if then
767  // else' case. there is an instruction before the branch.
768  StoreInst *OtherStore = nullptr;
769  if (OtherBr->isUnconditional()) {
770  --BBI;
771  // Skip over debugging info.
772  while (isa<DbgInfoIntrinsic>(BBI) ||
773  (isa<BitCastInst>(BBI) && BBI->getType()->isPointerTy())) {
774  if (BBI==OtherBB->begin())
775  return false;
776  --BBI;
777  }
778  // If this isn't a store, isn't a store to the same location, or is not the
779  // right kind of store, bail out.
780  OtherStore = dyn_cast<StoreInst>(BBI);
781  if (!OtherStore || OtherStore->getOperand(1) != SI.getOperand(1) ||
782  !SI.isSameOperationAs(OtherStore))
783  return false;
784  } else {
785  // Otherwise, the other block ended with a conditional branch. If one of the
786  // destinations is StoreBB, then we have the if/then case.
787  if (OtherBr->getSuccessor(0) != StoreBB &&
788  OtherBr->getSuccessor(1) != StoreBB)
789  return false;
790 
791  // Okay, we know that OtherBr now goes to Dest and StoreBB, so this is an
792  // if/then triangle. See if there is a store to the same ptr as SI that
793  // lives in OtherBB.
794  for (;; --BBI) {
795  // Check to see if we find the matching store.
796  if ((OtherStore = dyn_cast<StoreInst>(BBI))) {
797  if (OtherStore->getOperand(1) != SI.getOperand(1) ||
798  !SI.isSameOperationAs(OtherStore))
799  return false;
800  break;
801  }
802  // If we find something that may be using or overwriting the stored
803  // value, or if we run out of instructions, we can't do the xform.
804  if (BBI->mayReadFromMemory() || BBI->mayWriteToMemory() ||
805  BBI == OtherBB->begin())
806  return false;
807  }
808 
809  // In order to eliminate the store in OtherBr, we have to
810  // make sure nothing reads or overwrites the stored value in
811  // StoreBB.
812  for (BasicBlock::iterator I = StoreBB->begin(); &*I != &SI; ++I) {
813  // FIXME: This should really be AA driven.
814  if (I->mayReadFromMemory() || I->mayWriteToMemory())
815  return false;
816  }
817  }
818 
819  // Insert a PHI node now if we need it.
820  Value *MergedVal = OtherStore->getOperand(0);
821  if (MergedVal != SI.getOperand(0)) {
822  PHINode *PN = PHINode::Create(MergedVal->getType(), 2, "storemerge");
823  PN->addIncoming(SI.getOperand(0), SI.getParent());
824  PN->addIncoming(OtherStore->getOperand(0), OtherBB);
825  MergedVal = InsertNewInstBefore(PN, DestBB->front());
826  }
827 
828  // Advance to a place where it is safe to insert the new store and
829  // insert it.
830  BBI = DestBB->getFirstInsertionPt();
831  StoreInst *NewSI = new StoreInst(MergedVal, SI.getOperand(1),
832  SI.isVolatile(),
833  SI.getAlignment(),
834  SI.getOrdering(),
835  SI.getSynchScope());
836  InsertNewInstBefore(NewSI, *BBI);
837  NewSI->setDebugLoc(OtherStore->getDebugLoc());
838 
839  // If the two stores had the same TBAA tag, preserve it.
840  if (MDNode *TBAATag = SI.getMetadata(LLVMContext::MD_tbaa))
841  if ((TBAATag = MDNode::getMostGenericTBAA(TBAATag,
842  OtherStore->getMetadata(LLVMContext::MD_tbaa))))
843  NewSI->setMetadata(LLVMContext::MD_tbaa, TBAATag);
844 
845 
846  // Nuke the old stores.
848  EraseInstFromFunction(*OtherStore);
849  return true;
850 }
void addIncoming(Value *V, BasicBlock *BB)
bool isVolatile() const
Definition: Instructions.h:304
SynchronizationScope getSynchScope() const
Definition: Instructions.h:336
MDNode - a tuple of other values.
Definition: Metadata.h:74
const Instruction & front() const
Definition: BasicBlock.h:210
void setDebugLoc(const DebugLoc &Loc)
setDebugLoc - Set the debug location information for this instruction.
Definition: Instruction.h:193
iterator begin()
Definition: BasicBlock.h:198
bool isUnconditional() const
BasicBlock * getSuccessor(unsigned i) const
#define P(N)
unsigned getAlignment() const
Definition: Instructions.h:318
LLVM Basic Block Representation.
Definition: BasicBlock.h:72
BasicBlock * getSuccessor(unsigned idx) const
Definition: InstrTypes.h:65
Interval::pred_iterator pred_begin(Interval *I)
Definition: Interval.h:114
const DebugLoc & getDebugLoc() const
getDebugLoc - Return the debug location for this node as a DebugLoc.
Definition: Instruction.h:196
Value * getOperand(unsigned i) const
Definition: User.h:90
Interval::pred_iterator pred_end(Interval *I)
Definition: Interval.h:117
static MDNode * getMostGenericTBAA(MDNode *A, MDNode *B)
Methods for metadata merging.
void setMetadata(unsigned KindID, MDNode *Node)
Definition: Metadata.cpp:634
AtomicOrdering getOrdering() const
Returns the ordering effect of this store.
Definition: Instructions.h:325
Type * getType() const
Definition: Value.h:215
MDNode * getMetadata(unsigned KindID) const
Definition: Instruction.h:143
static PHINode * Create(Type *Ty, unsigned NumReservedValues, const Twine &NameStr="", Instruction *InsertBefore=nullptr)
LLVM_ATTRIBUTE_UNUSED_RESULT std::enable_if< !is_simple_type< Y >::value, typename cast_retty< X, const Y >::ret_type >::type dyn_cast(const Y &Val)
Definition: Casting.h:265
#define I(x, y, z)
Definition: MD5.cpp:54
TerminatorInst * getTerminator()
Returns the terminator instruction if the block is well formed or null if the block is not well forme...
Definition: BasicBlock.cpp:124
Instruction * InsertNewInstBefore(Instruction *New, Instruction &Old)
Definition: InstCombine.h:259
LLVM Value Representation.
Definition: Value.h:69
Instruction * EraseInstFromFunction(Instruction &I)
Definition: InstCombine.h:301
bool isSameOperationAs(const Instruction *I, unsigned flags=0) const
Determine if one instruction is the same operation as another.
iterator getFirstInsertionPt()
Returns an iterator to the first instruction in this block that is suitable for inserting a non-PHI i...
Definition: BasicBlock.cpp:174
const BasicBlock * getParent() const
Definition: Instruction.h:53
Value * InstCombiner::SimplifyUsingDistributiveLaws ( BinaryOperator I)
private

SimplifyUsingDistributiveLaws - This tries to simplify binary operations which some other binary operation distributes over either by factorizing out common terms (eg "(A*B)+(A*C)" -> "A*(B+C)") or expanding out if this results in simplifications (eg: "A & (B | C) -> (A&B) | (A&C)" if this is a win). Returns the simplified value, or null if it didn't simplify.

Definition at line 525 of file InstructionCombining.cpp.

525  {
526  Value *LHS = I.getOperand(0), *RHS = I.getOperand(1);
529 
530  // Factorization.
531  Value *A = nullptr, *B = nullptr, *C = nullptr, *D = nullptr;
532  Instruction::BinaryOps LHSOpcode = getBinOpsForFactorization(Op0, A, B);
534 
535  // The instruction has the form "(A op' B) op (C op' D)". Try to factorize
536  // a common term.
537  if (LHSOpcode == RHSOpcode) {
538  if (Value *V = tryFactorization(Builder, DL, I, LHSOpcode, A, B, C, D))
539  return V;
540  }
541 
542  // The instruction has the form "(A op' B) op (C)". Try to factorize common
543  // term.
544  if (Value *V = tryFactorization(Builder, DL, I, LHSOpcode, A, B, RHS,
545  getIdentityValue(LHSOpcode, RHS)))
546  return V;
547 
548  // The instruction has the form "(B) op (C op' D)". Try to factorize common
549  // term.
550  if (Value *V = tryFactorization(Builder, DL, I, RHSOpcode, LHS,
551  getIdentityValue(RHSOpcode, LHS), C, D))
552  return V;
553 
554  // Expansion.
555  Instruction::BinaryOps TopLevelOpcode = I.getOpcode();
556  if (Op0 && RightDistributesOverLeft(Op0->getOpcode(), TopLevelOpcode)) {
557  // The instruction has the form "(A op' B) op C". See if expanding it out
558  // to "(A op C) op' (B op C)" results in simplifications.
559  Value *A = Op0->getOperand(0), *B = Op0->getOperand(1), *C = RHS;
560  Instruction::BinaryOps InnerOpcode = Op0->getOpcode(); // op'
561 
562  // Do "A op C" and "B op C" both simplify?
563  if (Value *L = SimplifyBinOp(TopLevelOpcode, A, C, DL))
564  if (Value *R = SimplifyBinOp(TopLevelOpcode, B, C, DL)) {
565  // They do! Return "L op' R".
566  ++NumExpand;
567  // If "L op' R" equals "A op' B" then "L op' R" is just the LHS.
568  if ((L == A && R == B) ||
569  (Instruction::isCommutative(InnerOpcode) && L == B && R == A))
570  return Op0;
571  // Otherwise return "L op' R" if it simplifies.
572  if (Value *V = SimplifyBinOp(InnerOpcode, L, R, DL))
573  return V;
574  // Otherwise, create a new instruction.
575  C = Builder->CreateBinOp(InnerOpcode, L, R);
576  C->takeName(&I);
577  return C;
578  }
579  }
580 
581  if (Op1 && LeftDistributesOverRight(TopLevelOpcode, Op1->getOpcode())) {
582  // The instruction has the form "A op (B op' C)". See if expanding it out
583  // to "(A op B) op' (A op C)" results in simplifications.
584  Value *A = LHS, *B = Op1->getOperand(0), *C = Op1->getOperand(1);
585  Instruction::BinaryOps InnerOpcode = Op1->getOpcode(); // op'
586 
587  // Do "A op B" and "A op C" both simplify?
588  if (Value *L = SimplifyBinOp(TopLevelOpcode, A, B, DL))
589  if (Value *R = SimplifyBinOp(TopLevelOpcode, A, C, DL)) {
590  // They do! Return "L op' R".
591  ++NumExpand;
592  // If "L op' R" equals "B op' C" then "L op' R" is just the RHS.
593  if ((L == B && R == C) ||
594  (Instruction::isCommutative(InnerOpcode) && L == C && R == B))
595  return Op1;
596  // Otherwise return "L op' R" if it simplifies.
597  if (Value *V = SimplifyBinOp(InnerOpcode, L, R, DL))
598  return V;
599  // Otherwise, create a new instruction.
600  A = Builder->CreateBinOp(InnerOpcode, L, R);
601  A->takeName(&I);
602  return A;
603  }
604  }
605 
606  return nullptr;
607 }
static Instruction::BinaryOps getBinOpsForFactorization(BinaryOperator *Op, Value *&LHS, Value *&RHS)
static bool RightDistributesOverLeft(Instruction::BinaryOps LOp, Instruction::BinaryOps ROp)
Value * CreateBinOp(Instruction::BinaryOps Opc, Value *LHS, Value *RHS, const Twine &Name="", MDNode *FPMathTag=nullptr)
Definition: IRBuilder.h:851
void takeName(Value *V)
Definition: Value.cpp:242
static Value * tryFactorization(InstCombiner::BuilderTy *Builder, const DataLayout *DL, BinaryOperator &I, Instruction::BinaryOps InnerOpcode, Value *A, Value *B, Value *C, Value *D)
static Value * getIdentityValue(Instruction::BinaryOps OpCode, Value *V)
static bool LeftDistributesOverRight(Instruction::BinaryOps LOp, Instruction::BinaryOps ROp)
Value * getOperand(unsigned i) const
Definition: User.h:90
bool isCommutative() const
Definition: Instruction.h:287
BuilderTy * Builder
Definition: InstCombine.h:102
BinaryOps getOpcode() const
Definition: InstrTypes.h:326
Definition: test.h:1
Value * SimplifyBinOp(unsigned Opcode, Value *LHS, Value *RHS, const DataLayout *TD=nullptr, const TargetLibraryInfo *TLI=nullptr, const DominatorTree *DT=nullptr)
LLVM_ATTRIBUTE_UNUSED_RESULT std::enable_if< !is_simple_type< Y >::value, typename cast_retty< X, const Y >::ret_type >::type dyn_cast(const Y &Val)
Definition: Casting.h:265
const DataLayout * DL
Definition: InstCombine.h:89
LLVM Value Representation.
Definition: Value.h:69
Value * InstCombiner::SimplifyVectorOp ( BinaryOperator Inst)
private

Makes transformation of binary operation specific for vector types.

Parameters
InstBinary operator to transform.
Returns
Pointer to node that must replace the original binary operator, or null pointer if no transformation was made.

Definition at line 1202 of file InstructionCombining.cpp.

1202  {
1203  if (!Inst.getType()->isVectorTy()) return nullptr;
1204 
1205  // It may not be safe to reorder shuffles and things like div, urem, etc.
1206  // because we may trap when executing those ops on unknown vector elements.
1207  // See PR20059.
1208  if (!isSafeToSpeculativelyExecute(&Inst, DL)) return nullptr;
1209 
1210  unsigned VWidth = cast<VectorType>(Inst.getType())->getNumElements();
1211  Value *LHS = Inst.getOperand(0), *RHS = Inst.getOperand(1);
1212  assert(cast<VectorType>(LHS->getType())->getNumElements() == VWidth);
1213  assert(cast<VectorType>(RHS->getType())->getNumElements() == VWidth);
1214 
1215  // If both arguments of binary operation are shuffles, which use the same
1216  // mask and shuffle within a single vector, it is worthwhile to move the
1217  // shuffle after binary operation:
1218  // Op(shuffle(v1, m), shuffle(v2, m)) -> shuffle(Op(v1, v2), m)
1219  if (isa<ShuffleVectorInst>(LHS) && isa<ShuffleVectorInst>(RHS)) {
1220  ShuffleVectorInst *LShuf = cast<ShuffleVectorInst>(LHS);
1221  ShuffleVectorInst *RShuf = cast<ShuffleVectorInst>(RHS);
1222  if (isa<UndefValue>(LShuf->getOperand(1)) &&
1223  isa<UndefValue>(RShuf->getOperand(1)) &&
1224  LShuf->getOperand(0)->getType() == RShuf->getOperand(0)->getType() &&
1225  LShuf->getMask() == RShuf->getMask()) {
1226  Value *NewBO = CreateBinOpAsGiven(Inst, LShuf->getOperand(0),
1227  RShuf->getOperand(0), Builder);
1228  Value *Res = Builder->CreateShuffleVector(NewBO,
1229  UndefValue::get(NewBO->getType()), LShuf->getMask());
1230  return Res;
1231  }
1232  }
1233 
1234  // If one argument is a shuffle within one vector, the other is a constant,
1235  // try moving the shuffle after the binary operation.
1236  ShuffleVectorInst *Shuffle = nullptr;
1237  Constant *C1 = nullptr;
1238  if (isa<ShuffleVectorInst>(LHS)) Shuffle = cast<ShuffleVectorInst>(LHS);
1239  if (isa<ShuffleVectorInst>(RHS)) Shuffle = cast<ShuffleVectorInst>(RHS);
1240  if (isa<Constant>(LHS)) C1 = cast<Constant>(LHS);
1241  if (isa<Constant>(RHS)) C1 = cast<Constant>(RHS);
1242  if (Shuffle && C1 &&
1243  (isa<ConstantVector>(C1) || isa<ConstantDataVector>(C1)) &&
1244  isa<UndefValue>(Shuffle->getOperand(1)) &&
1245  Shuffle->getType() == Shuffle->getOperand(0)->getType()) {
1246  SmallVector<int, 16> ShMask = Shuffle->getShuffleMask();
1247  // Find constant C2 that has property:
1248  // shuffle(C2, ShMask) = C1
1249  // If such constant does not exist (example: ShMask=<0,0> and C1=<1,2>)
1250  // reorder is not possible.
1251  SmallVector<Constant*, 16> C2M(VWidth,
1253  bool MayChange = true;
1254  for (unsigned I = 0; I < VWidth; ++I) {
1255  if (ShMask[I] >= 0) {
1256  assert(ShMask[I] < (int)VWidth);
1257  if (!isa<UndefValue>(C2M[ShMask[I]])) {
1258  MayChange = false;
1259  break;
1260  }
1261  C2M[ShMask[I]] = C1->getAggregateElement(I);
1262  }
1263  }
1264  if (MayChange) {
1265  Constant *C2 = ConstantVector::get(C2M);
1266  Value *NewLHS, *NewRHS;
1267  if (isa<Constant>(LHS)) {
1268  NewLHS = C2;
1269  NewRHS = Shuffle->getOperand(0);
1270  } else {
1271  NewLHS = Shuffle->getOperand(0);
1272  NewRHS = C2;
1273  }
1274  Value *NewBO = CreateBinOpAsGiven(Inst, NewLHS, NewRHS, Builder);
1275  Value *Res = Builder->CreateShuffleVector(NewBO,
1276  UndefValue::get(Inst.getType()), Shuffle->getMask());
1277  return Res;
1278  }
1279  }
1280 
1281  return nullptr;
1282 }
Value * CreateShuffleVector(Value *V1, Value *V2, Value *Mask, const Twine &Name="")
Definition: IRBuilder.h:1407
static Value * CreateBinOpAsGiven(BinaryOperator &Inst, Value *LHS, Value *RHS, InstCombiner::BuilderTy *B)
Creates node of binary operation with the same attributes as the specified one but with other operand...
Constant * getMask() const
static Constant * get(ArrayRef< Constant * > V)
Definition: Constants.cpp:961
assert(Globals.size() > 1)
bool isVectorTy() const
Definition: Type.h:226
LLVM Constant Representation.
Definition: Constant.h:41
Value * getOperand(unsigned i) const
Definition: User.h:90
Constant * getAggregateElement(unsigned Elt) const
Definition: Constants.cpp:205
BuilderTy * Builder
Definition: InstCombine.h:102
static UndefValue * get(Type *T)
Definition: Constants.cpp:1372
Type * getType() const
Definition: Value.h:215
const Type * getScalarType() const LLVM_READONLY
Definition: Type.cpp:51
bool isSafeToSpeculativelyExecute(const Value *V, const DataLayout *TD=nullptr)
void Shuffle(internal::Random *random, std::vector< E > *v)
#define I(x, y, z)
Definition: MD5.cpp:54
VectorType * getType() const
const DataLayout * DL
Definition: InstCombine.h:89
LLVM Value Representation.
Definition: Value.h:69
static void getShuffleMask(Constant *Mask, SmallVectorImpl< int > &Result)
bool FunctionPass::skipOptnoneFunction ( const Function F) const
protectedinherited

skipOptnoneFunction - This function has Attribute::OptimizeNone and most transformation passes should skip it.

Definition at line 143 of file Pass.cpp.

143  {
145  DEBUG(dbgs() << "Skipping pass '" << getPassName()
146  << "' on function " << F.getName() << "\n");
147  return true;
148  }
149  return false;
150 }
virtual const char * getPassName() const
Definition: Pass.cpp:61
StringRef getName() const
Definition: Value.cpp:168
#define DEBUG(X)
raw_ostream & dbgs()
dbgs - Return a circular-buffered debug stream.
Function must not be optimized.
Definition: Attributes.h:96
bool hasFnAttribute(Attribute::AttrKind Kind) const
Return true if the function has the attribute.
Definition: Function.h:200
Instruction * InstCombiner::SliceUpIllegalIntegerPHI ( PHINode FirstPhi)

SliceUpIllegalIntegerPHI - This is an integer PHI and we know that it has an illegal type: see if it is only used by trunc or trunc(lshr) operations. If so, we split the PHI into the various pieces being extracted. This sort of thing is introduced when SROA promotes an aggregate to large integer values.

TODO: The user of the trunc may be an bitcast to float/double/vector or an inttoptr. We should produce new PHIs in the right type.

Definition at line 618 of file InstCombinePHI.cpp.

618  {
619  // PHIUsers - Keep track of all of the truncated values extracted from a set
620  // of PHIs, along with their offset. These are the things we want to rewrite.
622 
623  // PHIs are often mutually cyclic, so we keep track of a whole set of PHI
624  // nodes which are extracted from. PHIsToSlice is a set we use to avoid
625  // revisiting PHIs, PHIsInspected is a ordered list of PHIs that we need to
626  // check the uses of (to ensure they are all extracts).
627  SmallVector<PHINode*, 8> PHIsToSlice;
628  SmallPtrSet<PHINode*, 8> PHIsInspected;
629 
630  PHIsToSlice.push_back(&FirstPhi);
631  PHIsInspected.insert(&FirstPhi);
632 
633  for (unsigned PHIId = 0; PHIId != PHIsToSlice.size(); ++PHIId) {
634  PHINode *PN = PHIsToSlice[PHIId];
635 
636  // Scan the input list of the PHI. If any input is an invoke, and if the
637  // input is defined in the predecessor, then we won't be split the critical
638  // edge which is required to insert a truncate. Because of this, we have to
639  // bail out.
640  for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) {
642  if (!II) continue;
643  if (II->getParent() != PN->getIncomingBlock(i))
644  continue;
645 
646  // If we have a phi, and if it's directly in the predecessor, then we have
647  // a critical edge where we need to put the truncate. Since we can't
648  // split the edge in instcombine, we have to bail out.
649  return nullptr;
650  }
651 
652  for (User *U : PN->users()) {
653  Instruction *UserI = cast<Instruction>(U);
654 
655  // If the user is a PHI, inspect its uses recursively.
656  if (PHINode *UserPN = dyn_cast<PHINode>(UserI)) {
657  if (PHIsInspected.insert(UserPN))
658  PHIsToSlice.push_back(UserPN);
659  continue;
660  }
661 
662  // Truncates are always ok.
663  if (isa<TruncInst>(UserI)) {
664  PHIUsers.push_back(PHIUsageRecord(PHIId, 0, UserI));
665  continue;
666  }
667 
668  // Otherwise it must be a lshr which can only be used by one trunc.
669  if (UserI->getOpcode() != Instruction::LShr ||
670  !UserI->hasOneUse() || !isa<TruncInst>(UserI->user_back()) ||
671  !isa<ConstantInt>(UserI->getOperand(1)))
672  return nullptr;
673 
674  unsigned Shift = cast<ConstantInt>(UserI->getOperand(1))->getZExtValue();
675  PHIUsers.push_back(PHIUsageRecord(PHIId, Shift, UserI->user_back()));
676  }
677  }
678 
679  // If we have no users, they must be all self uses, just nuke the PHI.
680  if (PHIUsers.empty())
681  return ReplaceInstUsesWith(FirstPhi, UndefValue::get(FirstPhi.getType()));
682 
683  // If this phi node is transformable, create new PHIs for all the pieces
684  // extracted out of it. First, sort the users by their offset and size.
685  array_pod_sort(PHIUsers.begin(), PHIUsers.end());
686 
687  DEBUG(dbgs() << "SLICING UP PHI: " << FirstPhi << '\n';
688  for (unsigned i = 1, e = PHIsToSlice.size(); i != e; ++i)
689  dbgs() << "AND USER PHI #" << i << ": " << *PHIsToSlice[i] << '\n';
690  );
691 
692  // PredValues - This is a temporary used when rewriting PHI nodes. It is
693  // hoisted out here to avoid construction/destruction thrashing.
695 
696  // ExtractedVals - Each new PHI we introduce is saved here so we don't
697  // introduce redundant PHIs.
699 
700  for (unsigned UserI = 0, UserE = PHIUsers.size(); UserI != UserE; ++UserI) {
701  unsigned PHIId = PHIUsers[UserI].PHIId;
702  PHINode *PN = PHIsToSlice[PHIId];
703  unsigned Offset = PHIUsers[UserI].Shift;
704  Type *Ty = PHIUsers[UserI].Inst->getType();
705 
706  PHINode *EltPHI;
707 
708  // If we've already lowered a user like this, reuse the previously lowered
709  // value.
710  if ((EltPHI = ExtractedVals[LoweredPHIRecord(PN, Offset, Ty)]) == nullptr) {
711 
712  // Otherwise, Create the new PHI node for this user.
713  EltPHI = PHINode::Create(Ty, PN->getNumIncomingValues(),
714  PN->getName()+".off"+Twine(Offset), PN);
715  assert(EltPHI->getType() != PN->getType() &&
716  "Truncate didn't shrink phi?");
717 
718  for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) {
719  BasicBlock *Pred = PN->getIncomingBlock(i);
720  Value *&PredVal = PredValues[Pred];
721 
722  // If we already have a value for this predecessor, reuse it.
723  if (PredVal) {
724  EltPHI->addIncoming(PredVal, Pred);
725  continue;
726  }
727 
728  // Handle the PHI self-reuse case.
729  Value *InVal = PN->getIncomingValue(i);
730  if (InVal == PN) {
731  PredVal = EltPHI;
732  EltPHI->addIncoming(PredVal, Pred);
733  continue;
734  }
735 
736  if (PHINode *InPHI = dyn_cast<PHINode>(PN)) {
737  // If the incoming value was a PHI, and if it was one of the PHIs we
738  // already rewrote it, just use the lowered value.
739  if (Value *Res = ExtractedVals[LoweredPHIRecord(InPHI, Offset, Ty)]) {
740  PredVal = Res;
741  EltPHI->addIncoming(PredVal, Pred);
742  continue;
743  }
744  }
745 
746  // Otherwise, do an extract in the predecessor.
747  Builder->SetInsertPoint(Pred, Pred->getTerminator());
748  Value *Res = InVal;
749  if (Offset)
750  Res = Builder->CreateLShr(Res, ConstantInt::get(InVal->getType(),
751  Offset), "extract");
752  Res = Builder->CreateTrunc(Res, Ty, "extract.t");
753  PredVal = Res;
754  EltPHI->addIncoming(Res, Pred);
755 
756  // If the incoming value was a PHI, and if it was one of the PHIs we are
757  // rewriting, we will ultimately delete the code we inserted. This
758  // means we need to revisit that PHI to make sure we extract out the
759  // needed piece.
760  if (PHINode *OldInVal = dyn_cast<PHINode>(PN->getIncomingValue(i)))
761  if (PHIsInspected.count(OldInVal)) {
762  unsigned RefPHIId = std::find(PHIsToSlice.begin(),PHIsToSlice.end(),
763  OldInVal)-PHIsToSlice.begin();
764  PHIUsers.push_back(PHIUsageRecord(RefPHIId, Offset,
765  cast<Instruction>(Res)));
766  ++UserE;
767  }
768  }
769  PredValues.clear();
770 
771  DEBUG(dbgs() << " Made element PHI for offset " << Offset << ": "
772  << *EltPHI << '\n');
773  ExtractedVals[LoweredPHIRecord(PN, Offset, Ty)] = EltPHI;
774  }
775 
776  // Replace the use of this piece with the PHI node.
777  ReplaceInstUsesWith(*PHIUsers[UserI].Inst, EltPHI);
778  }
779 
780  // Replace all the remaining uses of the PHI nodes (self uses and the lshrs)
781  // with undefs.
782  Value *Undef = UndefValue::get(FirstPhi.getType());
783  for (unsigned i = 1, e = PHIsToSlice.size(); i != e; ++i)
784  ReplaceInstUsesWith(*PHIsToSlice[i], Undef);
785  return ReplaceInstUsesWith(FirstPhi, Undef);
786 }
Value * CreateLShr(Value *LHS, Value *RHS, const Twine &Name="", bool isExact=false)
Definition: IRBuilder.h:770
void addIncoming(Value *V, BasicBlock *BB)
size_type count(PtrType Ptr) const
count - Return 1 if the specified pointer is in the set, 0 otherwise.
Definition: SmallPtrSet.h:267
bool insert(PtrType Ptr)
Definition: SmallPtrSet.h:256
StringRef getName() const
Definition: Value.cpp:168
bool LLVM_ATTRIBUTE_UNUSED_RESULT empty() const
Definition: SmallVector.h:57
void SetInsertPoint(BasicBlock *TheBB)
This specifies that created instructions should be appended to the end of the specified block...
Definition: IRBuilder.h:83
unsigned getNumIncomingValues() const
void array_pod_sort(IteratorTy Start, IteratorTy End)
Definition: STLExtras.h:335
assert(Globals.size() > 1)
LLVM Basic Block Representation.
Definition: BasicBlock.h:72
Instruction * ReplaceInstUsesWith(Instruction &I, Value *V)
Definition: InstCombine.h:282
BasicBlock * getIncomingBlock(unsigned i) const
Value * getOperand(unsigned i) const
Definition: User.h:90
#define DEBUG(X)
BuilderTy * Builder
Definition: InstCombine.h:102
static UndefValue * get(Type *T)
Definition: Constants.cpp:1372
Value * getIncomingValue(unsigned i) const
Type * getType() const
Definition: Value.h:215
Instruction * user_back()
Definition: Instruction.h:50
static Constant * get(Type *Ty, uint64_t V, bool isSigned=false)
Definition: Constants.cpp:528
static PHINode * Create(Type *Ty, unsigned NumReservedValues, const Twine &NameStr="", Instruction *InsertBefore=nullptr)
raw_ostream & dbgs()
dbgs - Return a circular-buffered debug stream.
iterator_range< user_iterator > users()
Definition: Value.h:273
LLVM_ATTRIBUTE_UNUSED_RESULT std::enable_if< !is_simple_type< Y >::value, typename cast_retty< X, const Y >::ret_type >::type dyn_cast(const Y &Val)
Definition: Casting.h:265
TerminatorInst * getTerminator()
Returns the terminator instruction if the block is well formed or null if the block is not well forme...
Definition: BasicBlock.cpp:124
bool hasOneUse() const
Definition: Value.h:284
Value * CreateTrunc(Value *V, Type *DestTy, const Twine &Name="")
Definition: IRBuilder.h:1094
LLVM Value Representation.
Definition: Value.h:69
unsigned getOpcode() const
getOpcode() returns a member of one of the enums like Instruction::Add.
Definition: Instruction.h:86
const BasicBlock * getParent() const
Definition: Instruction.h:53
Instruction * InstCombiner::transformCallThroughTrampoline ( CallSite  CS,
IntrinsicInst Tramp 
)
private

Definition at line 1487 of file InstCombineCalls.cpp.

1488  {
1489  Value *Callee = CS.getCalledValue();
1490  PointerType *PTy = cast<PointerType>(Callee->getType());
1491  FunctionType *FTy = cast<FunctionType>(PTy->getElementType());
1492  const AttributeSet &Attrs = CS.getAttributes();
1493 
1494  // If the call already has the 'nest' attribute somewhere then give up -
1495  // otherwise 'nest' would occur twice after splicing in the chain.
1496  if (Attrs.hasAttrSomewhere(Attribute::Nest))
1497  return nullptr;
1498 
1499  assert(Tramp &&
1500  "transformCallThroughTrampoline called with incorrect CallSite.");
1501 
1502  Function *NestF =cast<Function>(Tramp->getArgOperand(1)->stripPointerCasts());
1503  PointerType *NestFPTy = cast<PointerType>(NestF->getType());
1504  FunctionType *NestFTy = cast<FunctionType>(NestFPTy->getElementType());
1505 
1506  const AttributeSet &NestAttrs = NestF->getAttributes();
1507  if (!NestAttrs.isEmpty()) {
1508  unsigned NestIdx = 1;
1509  Type *NestTy = nullptr;
1510  AttributeSet NestAttr;
1511 
1512  // Look for a parameter marked with the 'nest' attribute.
1513  for (FunctionType::param_iterator I = NestFTy->param_begin(),
1514  E = NestFTy->param_end(); I != E; ++NestIdx, ++I)
1515  if (NestAttrs.hasAttribute(NestIdx, Attribute::Nest)) {
1516  // Record the parameter type and any other attributes.
1517  NestTy = *I;
1518  NestAttr = NestAttrs.getParamAttributes(NestIdx);
1519  break;
1520  }
1521 
1522  if (NestTy) {
1523  Instruction *Caller = CS.getInstruction();
1524  std::vector<Value*> NewArgs;
1525  NewArgs.reserve(CS.arg_size() + 1);
1526 
1528  NewAttrs.reserve(Attrs.getNumSlots() + 1);
1529 
1530  // Insert the nest argument into the call argument list, which may
1531  // mean appending it. Likewise for attributes.
1532 
1533  // Add any result attributes.
1534  if (Attrs.hasAttributes(AttributeSet::ReturnIndex))
1535  NewAttrs.push_back(AttributeSet::get(Caller->getContext(),
1536  Attrs.getRetAttributes()));
1537 
1538  {
1539  unsigned Idx = 1;
1540  CallSite::arg_iterator I = CS.arg_begin(), E = CS.arg_end();
1541  do {
1542  if (Idx == NestIdx) {
1543  // Add the chain argument and attributes.
1544  Value *NestVal = Tramp->getArgOperand(2);
1545  if (NestVal->getType() != NestTy)
1546  NestVal = Builder->CreateBitCast(NestVal, NestTy, "nest");
1547  NewArgs.push_back(NestVal);
1548  NewAttrs.push_back(AttributeSet::get(Caller->getContext(),
1549  NestAttr));
1550  }
1551 
1552  if (I == E)
1553  break;
1554 
1555  // Add the original argument and attributes.
1556  NewArgs.push_back(*I);
1557  AttributeSet Attr = Attrs.getParamAttributes(Idx);
1558  if (Attr.hasAttributes(Idx)) {
1559  AttrBuilder B(Attr, Idx);
1560  NewAttrs.push_back(AttributeSet::get(Caller->getContext(),
1561  Idx + (Idx >= NestIdx), B));
1562  }
1563 
1564  ++Idx, ++I;
1565  } while (1);
1566  }
1567 
1568  // Add any function attributes.
1569  if (Attrs.hasAttributes(AttributeSet::FunctionIndex))
1570  NewAttrs.push_back(AttributeSet::get(FTy->getContext(),
1571  Attrs.getFnAttributes()));
1572 
1573  // The trampoline may have been bitcast to a bogus type (FTy).
1574  // Handle this by synthesizing a new function type, equal to FTy
1575  // with the chain parameter inserted.
1576 
1577  std::vector<Type*> NewTypes;
1578  NewTypes.reserve(FTy->getNumParams()+1);
1579 
1580  // Insert the chain's type into the list of parameter types, which may
1581  // mean appending it.
1582  {
1583  unsigned Idx = 1;
1584  FunctionType::param_iterator I = FTy->param_begin(),
1585  E = FTy->param_end();
1586 
1587  do {
1588  if (Idx == NestIdx)
1589  // Add the chain's type.
1590  NewTypes.push_back(NestTy);
1591 
1592  if (I == E)
1593  break;
1594 
1595  // Add the original type.
1596  NewTypes.push_back(*I);
1597 
1598  ++Idx, ++I;
1599  } while (1);
1600  }
1601 
1602  // Replace the trampoline call with a direct call. Let the generic
1603  // code sort out any function type mismatches.
1604  FunctionType *NewFTy = FunctionType::get(FTy->getReturnType(), NewTypes,
1605  FTy->isVarArg());
1606  Constant *NewCallee =
1607  NestF->getType() == PointerType::getUnqual(NewFTy) ?
1608  NestF : ConstantExpr::getBitCast(NestF,
1609  PointerType::getUnqual(NewFTy));
1610  const AttributeSet &NewPAL =
1611  AttributeSet::get(FTy->getContext(), NewAttrs);
1612 
1613  Instruction *NewCaller;
1614  if (InvokeInst *II = dyn_cast<InvokeInst>(Caller)) {
1615  NewCaller = InvokeInst::Create(NewCallee,
1616  II->getNormalDest(), II->getUnwindDest(),
1617  NewArgs);
1618  cast<InvokeInst>(NewCaller)->setCallingConv(II->getCallingConv());
1619  cast<InvokeInst>(NewCaller)->setAttributes(NewPAL);
1620  } else {
1621  NewCaller = CallInst::Create(NewCallee, NewArgs);
1622  if (cast<CallInst>(Caller)->isTailCall())
1623  cast<CallInst>(NewCaller)->setTailCall();
1624  cast<CallInst>(NewCaller)->
1625  setCallingConv(cast<CallInst>(Caller)->getCallingConv());
1626  cast<CallInst>(NewCaller)->setAttributes(NewPAL);
1627  }
1628 
1629  return NewCaller;
1630  }
1631  }
1632 
1633  // Replace the trampoline call with a direct call. Since there is no 'nest'
1634  // parameter, there is no need to adjust the argument list. Let the generic
1635  // code sort out any function type mismatches.
1636  Constant *NewCallee =
1637  NestF->getType() == PTy ? NestF :
1638  ConstantExpr::getBitCast(NestF, PTy);
1639  CS.setCalledFunction(NewCallee);
1640  return CS.getInstruction();
1641 }
void reserve(unsigned N)
Definition: SmallVector.h:401
const AttributeSet & getAttributes() const
Definition: CallSite.h:198
AttributeSet getParamAttributes(unsigned Index) const
The attributes for the specified index are returned.
Definition: Attributes.cpp:846
IterTy arg_end() const
Definition: CallSite.h:151
unsigned arg_size() const
Definition: CallSite.h:153
Nested function static chain.
Definition: Attributes.h:81
Type::subtype_iterator param_iterator
Definition: DerivedTypes.h:123
void setCalledFunction(Value *V)
Definition: CallSite.h:99
static CallInst * Create(Value *Func, ArrayRef< Value * > Args, const Twine &NameStr="", Instruction *InsertBefore=nullptr)
static AttributeSet get(LLVMContext &C, ArrayRef< std::pair< unsigned, Attribute > > Attrs)
Create an AttributeSet with the specified parameters in it.
Definition: Attributes.cpp:600
static FunctionType * get(Type *Result, ArrayRef< Type * > Params, bool isVarArg)
Definition: Type.cpp:355
ValTy * getCalledValue() const
Definition: CallSite.h:85
Type * getElementType() const
Definition: DerivedTypes.h:319
assert(Globals.size() > 1)
InstrTy * getInstruction() const
Definition: CallSite.h:79
LLVM Constant Representation.
Definition: Constant.h:41
BuilderTy * Builder
Definition: InstCombine.h:102
LLVMContext & getContext() const
All values hold a context through their type.
Definition: Value.cpp:615
static InvokeInst * Create(Value *Func, BasicBlock *IfNormal, BasicBlock *IfException, ArrayRef< Value * > Args, const Twine &NameStr="", Instruction *InsertBefore=nullptr)
static Constant * getBitCast(Constant *C, Type *Ty)
Definition: Constants.cpp:1721
static PointerType * getUnqual(Type *ElementType)
Definition: DerivedTypes.h:456
Value * CreateBitCast(Value *V, Type *DestTy, const Twine &Name="")
Definition: IRBuilder.h:1158
Type * getType() const
Definition: Value.h:215
Value * stripPointerCasts()
Strips off any unneeded pointer casts, all-zero GEPs and aliases from the specified value...
Definition: Value.cpp:423
AttributeSet getAttributes() const
Return the attribute list for this Function.
Definition: Function.h:170
Value * getArgOperand(unsigned i) const
PointerType * getType() const
Global values are always pointers.
Definition: GlobalValue.h:166
bool hasAttributes(unsigned Index) const
Return true if attribute exists at the given index.
Definition: Attributes.cpp:882
#define I(x, y, z)
Definition: MD5.cpp:54
IterTy arg_begin() const
Definition: CallSite.h:145
LLVM Value Representation.
Definition: Value.h:69
bool InstCombiner::transformConstExprCastCall ( CallSite  CS)
private

Definition at line 1227 of file InstCombineCalls.cpp.

1227  {
1228  Function *Callee =
1230  if (!Callee)
1231  return false;
1232  Instruction *Caller = CS.getInstruction();
1233  const AttributeSet &CallerPAL = CS.getAttributes();
1234 
1235  // Okay, this is a cast from a function to a different type. Unless doing so
1236  // would cause a type conversion of one of our arguments, change this call to
1237  // be a direct call with arguments casted to the appropriate types.
1238  //
1239  FunctionType *FT = Callee->getFunctionType();
1240  Type *OldRetTy = Caller->getType();
1241  Type *NewRetTy = FT->getReturnType();
1242 
1243  // Check to see if we are changing the return type...
1244  if (OldRetTy != NewRetTy) {
1245 
1246  if (NewRetTy->isStructTy())
1247  return false; // TODO: Handle multiple return values.
1248 
1249  if (!CastInst::isBitCastable(NewRetTy, OldRetTy)) {
1250  if (Callee->isDeclaration())
1251  return false; // Cannot transform this return value.
1252 
1253  if (!Caller->use_empty() &&
1254  // void -> non-void is handled specially
1255  !NewRetTy->isVoidTy())
1256  return false; // Cannot transform this return value.
1257  }
1258 
1259  if (!CallerPAL.isEmpty() && !Caller->use_empty()) {
1260  AttrBuilder RAttrs(CallerPAL, AttributeSet::ReturnIndex);
1261  if (RAttrs.
1262  hasAttributes(AttributeFuncs::
1265  return false; // Attribute not compatible with transformed value.
1266  }
1267 
1268  // If the callsite is an invoke instruction, and the return value is used by
1269  // a PHI node in a successor, we cannot change the return type of the call
1270  // because there is no place to put the cast instruction (without breaking
1271  // the critical edge). Bail out in this case.
1272  if (!Caller->use_empty())
1273  if (InvokeInst *II = dyn_cast<InvokeInst>(Caller))
1274  for (User *U : II->users())
1275  if (PHINode *PN = dyn_cast<PHINode>(U))
1276  if (PN->getParent() == II->getNormalDest() ||
1277  PN->getParent() == II->getUnwindDest())
1278  return false;
1279  }
1280 
1281  unsigned NumActualArgs = CS.arg_size();
1282  unsigned NumCommonArgs = std::min(FT->getNumParams(), NumActualArgs);
1283 
1285  for (unsigned i = 0, e = NumCommonArgs; i != e; ++i, ++AI) {
1286  Type *ParamTy = FT->getParamType(i);
1287  Type *ActTy = (*AI)->getType();
1288 
1289  if (!CastInst::isBitCastable(ActTy, ParamTy))
1290  return false; // Cannot transform this parameter value.
1291 
1292  if (AttrBuilder(CallerPAL.getParamAttributes(i + 1), i + 1).
1293  hasAttributes(AttributeFuncs::
1294  typeIncompatible(ParamTy, i + 1), i + 1))
1295  return false; // Attribute not compatible with transformed value.
1296 
1297  if (CS.isInAllocaArgument(i))
1298  return false; // Cannot transform to and from inalloca.
1299 
1300  // If the parameter is passed as a byval argument, then we have to have a
1301  // sized type and the sized type has to have the same size as the old type.
1302  if (ParamTy != ActTy &&
1303  CallerPAL.getParamAttributes(i + 1).hasAttribute(i + 1,
1304  Attribute::ByVal)) {
1305  PointerType *ParamPTy = dyn_cast<PointerType>(ParamTy);
1306  if (!ParamPTy || !ParamPTy->getElementType()->isSized() || !DL)
1307  return false;
1308 
1309  Type *CurElTy = ActTy->getPointerElementType();
1310  if (DL->getTypeAllocSize(CurElTy) !=
1311  DL->getTypeAllocSize(ParamPTy->getElementType()))
1312  return false;
1313  }
1314  }
1315 
1316  if (Callee->isDeclaration()) {
1317  // Do not delete arguments unless we have a function body.
1318  if (FT->getNumParams() < NumActualArgs && !FT->isVarArg())
1319  return false;
1320 
1321  // If the callee is just a declaration, don't change the varargsness of the
1322  // call. We don't want to introduce a varargs call where one doesn't
1323  // already exist.
1324  PointerType *APTy = cast<PointerType>(CS.getCalledValue()->getType());
1325  if (FT->isVarArg()!=cast<FunctionType>(APTy->getElementType())->isVarArg())
1326  return false;
1327 
1328  // If both the callee and the cast type are varargs, we still have to make
1329  // sure the number of fixed parameters are the same or we have the same
1330  // ABI issues as if we introduce a varargs call.
1331  if (FT->isVarArg() &&
1332  cast<FunctionType>(APTy->getElementType())->isVarArg() &&
1333  FT->getNumParams() !=
1334  cast<FunctionType>(APTy->getElementType())->getNumParams())
1335  return false;
1336  }
1337 
1338  if (FT->getNumParams() < NumActualArgs && FT->isVarArg() &&
1339  !CallerPAL.isEmpty())
1340  // In this case we have more arguments than the new function type, but we
1341  // won't be dropping them. Check that these extra arguments have attributes
1342  // that are compatible with being a vararg call argument.
1343  for (unsigned i = CallerPAL.getNumSlots(); i; --i) {
1344  unsigned Index = CallerPAL.getSlotIndex(i - 1);
1345  if (Index <= FT->getNumParams())
1346  break;
1347 
1348  // Check if it has an attribute that's incompatible with varargs.
1349  AttributeSet PAttrs = CallerPAL.getSlotAttributes(i - 1);
1350  if (PAttrs.hasAttribute(Index, Attribute::StructRet))
1351  return false;
1352  }
1353 
1354 
1355  // Okay, we decided that this is a safe thing to do: go ahead and start
1356  // inserting cast instructions as necessary.
1357  std::vector<Value*> Args;
1358  Args.reserve(NumActualArgs);
1360  attrVec.reserve(NumCommonArgs);
1361 
1362  // Get any return attributes.
1363  AttrBuilder RAttrs(CallerPAL, AttributeSet::ReturnIndex);
1364 
1365  // If the return value is not being used, the type may not be compatible
1366  // with the existing attributes. Wipe out any problematic attributes.
1367  RAttrs.
1368  removeAttributes(AttributeFuncs::
1371 
1372  // Add the new return attributes.
1373  if (RAttrs.hasAttributes())
1374  attrVec.push_back(AttributeSet::get(Caller->getContext(),
1375  AttributeSet::ReturnIndex, RAttrs));
1376 
1377  AI = CS.arg_begin();
1378  for (unsigned i = 0; i != NumCommonArgs; ++i, ++AI) {
1379  Type *ParamTy = FT->getParamType(i);
1380 
1381  if ((*AI)->getType() == ParamTy) {
1382  Args.push_back(*AI);
1383  } else {
1384  Args.push_back(Builder->CreateBitCast(*AI, ParamTy));
1385  }
1386 
1387  // Add any parameter attributes.
1388  AttrBuilder PAttrs(CallerPAL.getParamAttributes(i + 1), i + 1);
1389  if (PAttrs.hasAttributes())
1390  attrVec.push_back(AttributeSet::get(Caller->getContext(), i + 1,
1391  PAttrs));
1392  }
1393 
1394  // If the function takes more arguments than the call was taking, add them
1395  // now.
1396  for (unsigned i = NumCommonArgs; i != FT->getNumParams(); ++i)
1397  Args.push_back(Constant::getNullValue(FT->getParamType(i)));
1398 
1399  // If we are removing arguments to the function, emit an obnoxious warning.
1400  if (FT->getNumParams() < NumActualArgs) {
1401  // TODO: if (!FT->isVarArg()) this call may be unreachable. PR14722
1402  if (FT->isVarArg()) {
1403  // Add all of the arguments in their promoted form to the arg list.
1404  for (unsigned i = FT->getNumParams(); i != NumActualArgs; ++i, ++AI) {
1405  Type *PTy = getPromotedType((*AI)->getType());
1406  if (PTy != (*AI)->getType()) {
1407  // Must promote to pass through va_arg area!
1408  Instruction::CastOps opcode =
1409  CastInst::getCastOpcode(*AI, false, PTy, false);
1410  Args.push_back(Builder->CreateCast(opcode, *AI, PTy));
1411  } else {
1412  Args.push_back(*AI);
1413  }
1414 
1415  // Add any parameter attributes.
1416  AttrBuilder PAttrs(CallerPAL.getParamAttributes(i + 1), i + 1);
1417  if (PAttrs.hasAttributes())
1418  attrVec.push_back(AttributeSet::get(FT->getContext(), i + 1,
1419  PAttrs));
1420  }
1421  }
1422  }
1423 
1424  AttributeSet FnAttrs = CallerPAL.getFnAttributes();
1426  attrVec.push_back(AttributeSet::get(Callee->getContext(), FnAttrs));
1427 
1428  if (NewRetTy->isVoidTy())
1429  Caller->setName(""); // Void type should not have a name.
1430 
1431  const AttributeSet &NewCallerPAL = AttributeSet::get(Callee->getContext(),
1432  attrVec);
1433 
1434  Instruction *NC;
1435  if (InvokeInst *II = dyn_cast<InvokeInst>(Caller)) {
1436  NC = Builder->CreateInvoke(Callee, II->getNormalDest(),
1437  II->getUnwindDest(), Args);
1438  NC->takeName(II);
1439  cast<InvokeInst>(NC)->setCallingConv(II->getCallingConv());
1440  cast<InvokeInst>(NC)->setAttributes(NewCallerPAL);
1441  } else {
1442  CallInst *CI = cast<CallInst>(Caller);
1443  NC = Builder->CreateCall(Callee, Args);
1444  NC->takeName(CI);
1445  if (CI->isTailCall())
1446  cast<CallInst>(NC)->setTailCall();
1447  cast<CallInst>(NC)->setCallingConv(CI->getCallingConv());
1448  cast<CallInst>(NC)->setAttributes(NewCallerPAL);
1449  }
1450 
1451  // Insert a cast of the return type as necessary.
1452  Value *NV = NC;
1453  if (OldRetTy != NV->getType() && !Caller->use_empty()) {
1454  if (!NV->getType()->isVoidTy()) {
1455  NV = NC = CastInst::Create(CastInst::BitCast, NC, OldRetTy);
1456  NC->setDebugLoc(Caller->getDebugLoc());
1457 
1458  // If this is an invoke instruction, we should insert it after the first
1459  // non-phi, instruction in the normal successor block.
1460  if (InvokeInst *II = dyn_cast<InvokeInst>(Caller)) {
1461  BasicBlock::iterator I = II->getNormalDest()->getFirstInsertionPt();
1462  InsertNewInstBefore(NC, *I);
1463  } else {
1464  // Otherwise, it's a call, just insert cast right after the call.
1465  InsertNewInstBefore(NC, *Caller);
1466  }
1467  Worklist.AddUsersToWorkList(*Caller);
1468  } else {
1469  NV = UndefValue::get(Caller->getType());
1470  }
1471  }
1472 
1473  if (!Caller->use_empty())
1474  ReplaceInstUsesWith(*Caller, NV);
1475  else if (Caller->hasValueHandle())
1476  ValueHandleBase::ValueIsRAUWd(Caller, NV);
1477 
1478  EraseInstFromFunction(*Caller);
1479  return true;
1480 }
Value * CreateCast(Instruction::CastOps Op, Value *V, Type *DestTy, const Twine &Name="")
Definition: IRBuilder.h:1190
void reserve(unsigned N)
Definition: SmallVector.h:401
LLVMContext & getContext() const
Definition: Function.cpp:202
const AttributeSet & getAttributes() const
Definition: CallSite.h:198
AttributeSet getParamAttributes(unsigned Index) const
The attributes for the specified index are returned.
Definition: Attributes.cpp:846
unsigned getNumParams() const
Definition: DerivedTypes.h:133
unsigned arg_size() const
Definition: CallSite.h:153
bool hasAttribute(unsigned Index, Attribute::AttrKind Kind) const
Return true if the attribute exists at the given index.
Definition: Attributes.cpp:872
Type * getPointerElementType() const
Definition: Type.h:363
static Constant * getNullValue(Type *Ty)
Definition: Constants.cpp:133
static AttributeSet get(LLVMContext &C, ArrayRef< std::pair< unsigned, Attribute > > Attrs)
Create an AttributeSet with the specified parameters in it.
Definition: Attributes.cpp:600
void setName(const Twine &Name)
Definition: Value.cpp:176
AttributeSet typeIncompatible(Type *Ty, uint64_t Index)
Which attributes cannot be applied to a type.
Hidden pointer to structure to return.
Definition: Attributes.h:109
LLVMContext & getContext() const
getContext - Return the LLVMContext in which this type was uniqued.
Definition: Type.h:125
Pass structure by value.
Definition: Attributes.h:73
ValTy * getCalledValue() const
Definition: CallSite.h:85
static void ValueIsRAUWd(Value *Old, Value *New)
Definition: Value.cpp:775
void takeName(Value *V)
Definition: Value.cpp:242
Type * getElementType() const
Definition: DerivedTypes.h:319
unsigned getNumSlots() const
Return the number of slots used in this attribute list. This is the number of arguments that have an ...
Definition: Attributes.cpp:965
Type * getParamType(unsigned i) const
Parameter type accessors.
Definition: DerivedTypes.h:128
InstrTy * getInstruction() const
Definition: CallSite.h:79
void AddUsersToWorkList(Instruction &I)
AttributeSet getSlotAttributes(unsigned Slot) const
Return the attributes at the given slot.
Definition: Attributes.cpp:975
Instruction * ReplaceInstUsesWith(Instruction &I, Value *V)
Definition: InstCombine.h:282
const DebugLoc & getDebugLoc() const
getDebugLoc - Return the debug location for this node as a DebugLoc.
Definition: Instruction.h:196
static bool isBitCastable(Type *SrcTy, Type *DestTy)
Check whether a bitcast between these types is valid.
bool hasValueHandle() const
Definition: Value.h:384
InstCombineWorklist Worklist
Worklist - All of the instructions that need to be simplified.
Definition: InstCombine.h:97
BuilderTy * Builder
Definition: InstCombine.h:102
static UndefValue * get(Type *T)
Definition: Constants.cpp:1372
LLVMContext & getContext() const
All values hold a context through their type.
Definition: Value.cpp:615
bool isInAllocaArgument(unsigned ArgNo) const
Determine whether this argument is passed in an alloca.
Definition: CallSite.h:286
const char * Args[]
uint64_t getTypeAllocSize(Type *Ty) const
Definition: DataLayout.h:368
Value * CreateBitCast(Value *V, Type *DestTy, const Twine &Name="")
Definition: IRBuilder.h:1158
Type * getType() const
Definition: Value.h:215
Value * stripPointerCasts()
Strips off any unneeded pointer casts, all-zero GEPs and aliases from the specified value...
Definition: Value.cpp:423
#define NC
Definition: regutils.h:42
bool isSized(SmallPtrSet< const Type *, 4 > *Visited=nullptr) const
Definition: Type.h:268
iterator_range< user_iterator > users()
Definition: Value.h:273
LLVM_ATTRIBUTE_UNUSED_RESULT std::enable_if< !is_simple_type< Y >::value, typename cast_retty< X, const Y >::ret_type >::type dyn_cast(const Y &Val)
Definition: Casting.h:265
bool isStructTy() const
Definition: Type.h:209
static CastInst * Create(Instruction::CastOps, Value *S, Type *Ty, const Twine &Name="", Instruction *InsertBefore=nullptr)
Construct any of the CastInst subclasses.
bool isDeclaration() const
Definition: Globals.cpp:113
unsigned getSlotIndex(unsigned Slot) const
Return the index for the given slot.
Definition: Attributes.cpp:969
bool hasAttributes(unsigned Index) const
Return true if attribute exists at the given index.
Definition: Attributes.cpp:882
static Instruction::CastOps getCastOpcode(const Value *Val, bool SrcIsSigned, Type *Ty, bool DstIsSigned)
Infer the opcode for cast operand and type.
#define I(x, y, z)
Definition: MD5.cpp:54
FunctionType * getFunctionType() const
Definition: Function.cpp:206
bool isTailCall() const
Instruction * InsertNewInstBefore(Instruction *New, Instruction &Old)
Definition: InstCombine.h:259
IterTy arg_begin() const
Definition: CallSite.h:145
bool isVarArg() const
Definition: DerivedTypes.h:120
bool use_empty() const
Definition: Value.h:250
const DataLayout * DL
Definition: InstCombine.h:89
Type * getReturnType() const
Definition: DerivedTypes.h:121
LLVM Value Representation.
Definition: Value.h:69
CallInst * CreateCall(Value *Callee, const Twine &Name="")
Definition: IRBuilder.h:1345
Instruction * EraseInstFromFunction(Instruction &I)
Definition: InstCombine.h:301
CallingConv::ID getCallingConv() const
InvokeInst * CreateInvoke(Value *Callee, BasicBlock *NormalDest, BasicBlock *UnwindDest, const Twine &Name="")
Definition: IRBuilder.h:571
bool isEmpty() const
Return true if there are no attributes.
Definition: Attributes.h:358
static Type * getPromotedType(Type *Ty)
bool isVoidTy() const
isVoidTy - Return true if this is 'void'.
Definition: Type.h:137
AttributeSet getFnAttributes() const
The function attributes are returned.
Definition: Attributes.cpp:863
Instruction * InstCombiner::transformSExtICmp ( ICmpInst ICI,
Instruction CI 
)
private

transformSExtICmp - Transform (sext icmp) to bitwise / integer operations in order to eliminate the icmp.

Definition at line 894 of file InstCombineCasts.cpp.

894  {
895  Value *Op0 = ICI->getOperand(0), *Op1 = ICI->getOperand(1);
896  ICmpInst::Predicate Pred = ICI->getPredicate();
897 
898  if (Constant *Op1C = dyn_cast<Constant>(Op1)) {
899  // (x <s 0) ? -1 : 0 -> ashr x, 31 -> all ones if negative
900  // (x >s -1) ? -1 : 0 -> not (ashr x, 31) -> all ones if positive
901  if ((Pred == ICmpInst::ICMP_SLT && Op1C->isNullValue()) ||
902  (Pred == ICmpInst::ICMP_SGT && Op1C->isAllOnesValue())) {
903 
904  Value *Sh = ConstantInt::get(Op0->getType(),
905  Op0->getType()->getScalarSizeInBits()-1);
906  Value *In = Builder->CreateAShr(Op0, Sh, Op0->getName()+".lobit");
907  if (In->getType() != CI.getType())
908  In = Builder->CreateIntCast(In, CI.getType(), true/*SExt*/);
909 
910  if (Pred == ICmpInst::ICMP_SGT)
911  In = Builder->CreateNot(In, In->getName()+".not");
912  return ReplaceInstUsesWith(CI, In);
913  }
914  }
915 
916  if (ConstantInt *Op1C = dyn_cast<ConstantInt>(Op1)) {
917  // If we know that only one bit of the LHS of the icmp can be set and we
918  // have an equality comparison with zero or a power of 2, we can transform
919  // the icmp and sext into bitwise/integer operations.
920  if (ICI->hasOneUse() &&
921  ICI->isEquality() && (Op1C->isZero() || Op1C->getValue().isPowerOf2())){
922  unsigned BitWidth = Op1C->getType()->getBitWidth();
923  APInt KnownZero(BitWidth, 0), KnownOne(BitWidth, 0);
924  computeKnownBits(Op0, KnownZero, KnownOne);
925 
926  APInt KnownZeroMask(~KnownZero);
927  if (KnownZeroMask.isPowerOf2()) {
928  Value *In = ICI->getOperand(0);
929 
930  // If the icmp tests for a known zero bit we can constant fold it.
931  if (!Op1C->isZero() && Op1C->getValue() != KnownZeroMask) {
932  Value *V = Pred == ICmpInst::ICMP_NE ?
935  return ReplaceInstUsesWith(CI, V);
936  }
937 
938  if (!Op1C->isZero() == (Pred == ICmpInst::ICMP_NE)) {
939  // sext ((x & 2^n) == 0) -> (x >> n) - 1
940  // sext ((x & 2^n) != 2^n) -> (x >> n) - 1
941  unsigned ShiftAmt = KnownZeroMask.countTrailingZeros();
942  // Perform a right shift to place the desired bit in the LSB.
943  if (ShiftAmt)
944  In = Builder->CreateLShr(In,
945  ConstantInt::get(In->getType(), ShiftAmt));
946 
947  // At this point "In" is either 1 or 0. Subtract 1 to turn
948  // {1, 0} -> {0, -1}.
949  In = Builder->CreateAdd(In,
951  "sext");
952  } else {
953  // sext ((x & 2^n) != 0) -> (x << bitwidth-n) a>> bitwidth-1
954  // sext ((x & 2^n) == 2^n) -> (x << bitwidth-n) a>> bitwidth-1
955  unsigned ShiftAmt = KnownZeroMask.countLeadingZeros();
956  // Perform a left shift to place the desired bit in the MSB.
957  if (ShiftAmt)
958  In = Builder->CreateShl(In,
959  ConstantInt::get(In->getType(), ShiftAmt));
960 
961  // Distribute the bit over the whole bit width.
962  In = Builder->CreateAShr(In, ConstantInt::get(In->getType(),
963  BitWidth - 1), "sext");
964  }
965 
966  if (CI.getType() == In->getType())
967  return ReplaceInstUsesWith(CI, In);
968  return CastInst::CreateIntegerCast(In, CI.getType(), true/*SExt*/);
969  }
970  }
971  }
972 
973  return nullptr;
974 }
Value * CreateLShr(Value *LHS, Value *RHS, const Twine &Name="", bool isExact=false)
Definition: IRBuilder.h:770
Value * CreateIntCast(Value *V, Type *DestTy, bool isSigned, const Twine &Name="")
Definition: IRBuilder.h:1221
static bool isEquality(Predicate P)
static Constant * getNullValue(Type *Ty)
Definition: Constants.cpp:133
StringRef getName() const
Definition: Value.cpp:168
Value * CreateAShr(Value *LHS, Value *RHS, const Twine &Name="", bool isExact=false)
Definition: IRBuilder.h:788
Value * CreateNot(Value *V, const Twine &Name="")
Definition: IRBuilder.h:885
Value * CreateAdd(Value *LHS, Value *RHS, const Twine &Name="", bool HasNUW=false, bool HasNSW=false)
Definition: IRBuilder.h:632
LLVM Constant Representation.
Definition: Constant.h:41
Instruction * ReplaceInstUsesWith(Instruction &I, Value *V)
Definition: InstCombine.h:282
Value * getOperand(unsigned i) const
Definition: User.h:90
Predicate getPredicate() const
Return the predicate for this instruction.
Definition: InstrTypes.h:735
static Constant * getAllOnesValue(Type *Ty)
Get the all ones value.
Definition: Constants.cpp:185
BuilderTy * Builder
Definition: InstCombine.h:102
signed greater than
Definition: InstrTypes.h:699
static CastInst * CreateIntegerCast(Value *S, Type *Ty, bool isSigned, const Twine &Name="", Instruction *InsertBefore=nullptr)
Create a ZExt, BitCast, or Trunc for int -> int casts.
Class for constant integers.
Definition: Constants.h:51
unsigned getScalarSizeInBits() const LLVM_READONLY
Definition: Type.cpp:135
Type * getType() const
Definition: Value.h:215
signed less than
Definition: InstrTypes.h:701
static Constant * get(Type *Ty, uint64_t V, bool isSigned=false)
Definition: Constants.cpp:528
Class for arbitrary precision integers.
Definition: APInt.h:75
Value * CreateShl(Value *LHS, Value *RHS, const Twine &Name="", bool HasNUW=false, bool HasNSW=false)
Definition: IRBuilder.h:751
bool hasOneUse() const
Definition: Value.h:284
LLVM Value Representation.
Definition: Value.h:69
void computeKnownBits(Value *V, APInt &KnownZero, APInt &KnownOne, unsigned Depth=0) const
Definition: InstCombine.h:318
Instruction * InstCombiner::transformZExtICmp ( ICmpInst ICI,
Instruction CI,
bool  DoXform = true 
)
private

transformZExtICmp - Transform (zext icmp) to bitwise / integer operations in order to eliminate the icmp.

Definition at line 513 of file InstCombineCasts.cpp.

514  {
515  // If we are just checking for a icmp eq of a single bit and zext'ing it
516  // to an integer, then shift the bit to the appropriate place and then
517  // cast to integer to avoid the comparison.
518  if (ConstantInt *Op1C = dyn_cast<ConstantInt>(ICI->getOperand(1))) {
519  const APInt &Op1CV = Op1C->getValue();
520 
521  // zext (x <s 0) to i32 --> x>>u31 true if signbit set.
522  // zext (x >s -1) to i32 --> (x>>u31)^1 true if signbit clear.
523  if ((ICI->getPredicate() == ICmpInst::ICMP_SLT && Op1CV == 0) ||
524  (ICI->getPredicate() == ICmpInst::ICMP_SGT &&Op1CV.isAllOnesValue())) {
525  if (!DoXform) return ICI;
526 
527  Value *In = ICI->getOperand(0);
528  Value *Sh = ConstantInt::get(In->getType(),
529  In->getType()->getScalarSizeInBits()-1);
530  In = Builder->CreateLShr(In, Sh, In->getName()+".lobit");
531  if (In->getType() != CI.getType())
532  In = Builder->CreateIntCast(In, CI.getType(), false/*ZExt*/);
533 
534  if (ICI->getPredicate() == ICmpInst::ICMP_SGT) {
535  Constant *One = ConstantInt::get(In->getType(), 1);
536  In = Builder->CreateXor(In, One, In->getName()+".not");
537  }
538 
539  return ReplaceInstUsesWith(CI, In);
540  }
541 
542  // zext (X == 0) to i32 --> X^1 iff X has only the low bit set.
543  // zext (X == 0) to i32 --> (X>>1)^1 iff X has only the 2nd bit set.
544  // zext (X == 1) to i32 --> X iff X has only the low bit set.
545  // zext (X == 2) to i32 --> X>>1 iff X has only the 2nd bit set.
546  // zext (X != 0) to i32 --> X iff X has only the low bit set.
547  // zext (X != 0) to i32 --> X>>1 iff X has only the 2nd bit set.
548  // zext (X != 1) to i32 --> X^1 iff X has only the low bit set.
549  // zext (X != 2) to i32 --> (X>>1)^1 iff X has only the 2nd bit set.
550  if ((Op1CV == 0 || Op1CV.isPowerOf2()) &&
551  // This only works for EQ and NE
552  ICI->isEquality()) {
553  // If Op1C some other power of two, convert:
554  uint32_t BitWidth = Op1C->getType()->getBitWidth();
555  APInt KnownZero(BitWidth, 0), KnownOne(BitWidth, 0);
556  computeKnownBits(ICI->getOperand(0), KnownZero, KnownOne);
557 
558  APInt KnownZeroMask(~KnownZero);
559  if (KnownZeroMask.isPowerOf2()) { // Exactly 1 possible 1?
560  if (!DoXform) return ICI;
561 
562  bool isNE = ICI->getPredicate() == ICmpInst::ICMP_NE;
563  if (Op1CV != 0 && (Op1CV != KnownZeroMask)) {
564  // (X&4) == 2 --> false
565  // (X&4) != 2 --> true
567  isNE);
568  Res = ConstantExpr::getZExt(Res, CI.getType());
569  return ReplaceInstUsesWith(CI, Res);
570  }
571 
572  uint32_t ShiftAmt = KnownZeroMask.logBase2();
573  Value *In = ICI->getOperand(0);
574  if (ShiftAmt) {
575  // Perform a logical shr by shiftamt.
576  // Insert the shift to put the result in the low bit.
577  In = Builder->CreateLShr(In, ConstantInt::get(In->getType(),ShiftAmt),
578  In->getName()+".lobit");
579  }
580 
581  if ((Op1CV != 0) == isNE) { // Toggle the low bit.
582  Constant *One = ConstantInt::get(In->getType(), 1);
583  In = Builder->CreateXor(In, One);
584  }
585 
586  if (CI.getType() == In->getType())
587  return ReplaceInstUsesWith(CI, In);
588  return CastInst::CreateIntegerCast(In, CI.getType(), false/*ZExt*/);
589  }
590  }
591  }
592 
593  // icmp ne A, B is equal to xor A, B when A and B only really have one bit.
594  // It is also profitable to transform icmp eq into not(xor(A, B)) because that
595  // may lead to additional simplifications.
596  if (ICI->isEquality() && CI.getType() == ICI->getOperand(0)->getType()) {
597  if (IntegerType *ITy = dyn_cast<IntegerType>(CI.getType())) {
598  uint32_t BitWidth = ITy->getBitWidth();
599  Value *LHS = ICI->getOperand(0);
600  Value *RHS = ICI->getOperand(1);
601 
602  APInt KnownZeroLHS(BitWidth, 0), KnownOneLHS(BitWidth, 0);
603  APInt KnownZeroRHS(BitWidth, 0), KnownOneRHS(BitWidth, 0);
604  computeKnownBits(LHS, KnownZeroLHS, KnownOneLHS);
605  computeKnownBits(RHS, KnownZeroRHS, KnownOneRHS);
606 
607  if (KnownZeroLHS == KnownZeroRHS && KnownOneLHS == KnownOneRHS) {
608  APInt KnownBits = KnownZeroLHS | KnownOneLHS;
609  APInt UnknownBit = ~KnownBits;
610  if (UnknownBit.countPopulation() == 1) {
611  if (!DoXform) return ICI;
612 
613  Value *Result = Builder->CreateXor(LHS, RHS);
614 
615  // Mask off any bits that are set and won't be shifted away.
616  if (KnownOneLHS.uge(UnknownBit))
617  Result = Builder->CreateAnd(Result,
618  ConstantInt::get(ITy, UnknownBit));
619 
620  // Shift the bit we're testing down to the lsb.
621  Result = Builder->CreateLShr(
622  Result, ConstantInt::get(ITy, UnknownBit.countTrailingZeros()));
623 
624  if (ICI->getPredicate() == ICmpInst::ICMP_EQ)
625  Result = Builder->CreateXor(Result, ConstantInt::get(ITy, 1));
626  Result->takeName(ICI);
627  return ReplaceInstUsesWith(CI, Result);
628  }
629  }
630  }
631  }
632 
633  return nullptr;
634 }
Value * CreateLShr(Value *LHS, Value *RHS, const Twine &Name="", bool isExact=false)
Definition: IRBuilder.h:770
static IntegerType * getInt1Ty(LLVMContext &C)
Definition: Type.cpp:232
Value * CreateIntCast(Value *V, Type *DestTy, bool isSigned, const Twine &Name="")
Definition: IRBuilder.h:1221
static bool isEquality(Predicate P)
StringRef getName() const
Definition: Value.cpp:168
Value * CreateXor(Value *LHS, Value *RHS, const Twine &Name="")
Definition: IRBuilder.h:838
Value * CreateAnd(Value *LHS, Value *RHS, const Twine &Name="")
Definition: IRBuilder.h:806
void takeName(Value *V)
Definition: Value.cpp:242
LLVM Constant Representation.
Definition: Constant.h:41
Instruction * ReplaceInstUsesWith(Instruction &I, Value *V)
Definition: InstCombine.h:282
Value * getOperand(unsigned i) const
Definition: User.h:90
Integer representation type.
Definition: DerivedTypes.h:37
unsigned countPopulation() const
Count the number of bits set.
Definition: APInt.h:1390
Predicate getPredicate() const
Return the predicate for this instruction.
Definition: InstrTypes.h:735
BuilderTy * Builder
Definition: InstCombine.h:102
LLVMContext & getContext() const
All values hold a context through their type.
Definition: Value.cpp:615
signed greater than
Definition: InstrTypes.h:699
unsigned countTrailingZeros() const
Count the number of trailing zero bits.
Definition: APInt.cpp:737
static CastInst * CreateIntegerCast(Value *S, Type *Ty, bool isSigned, const Twine &Name="", Instruction *InsertBefore=nullptr)
Create a ZExt, BitCast, or Trunc for int -> int casts.
Class for constant integers.
Definition: Constants.h:51
unsigned getScalarSizeInBits() const LLVM_READONLY
Definition: Type.cpp:135
Type * getType() const
Definition: Value.h:215
signed less than
Definition: InstrTypes.h:701
static Constant * get(Type *Ty, uint64_t V, bool isSigned=false)
Definition: Constants.cpp:528
Class for arbitrary precision integers.
Definition: APInt.h:75
static Constant * getZExt(Constant *C, Type *Ty)
Definition: Constants.cpp:1615
LLVM Value Representation.
Definition: Value.h:69
void computeKnownBits(Value *V, APInt &KnownZero, APInt &KnownOne, unsigned Depth=0) const
Definition: InstCombine.h:318
Instruction * InstCombiner::tryOptimizeCall ( CallInst CI,
const DataLayout DL 
)
private

Definition at line 1037 of file InstCombineCalls.cpp.

1037  {
1038  if (!CI->getCalledFunction()) return nullptr;
1039 
1040  if (Value *With = Simplifier->optimizeCall(CI)) {
1041  ++NumSimplified;
1042  return CI->use_empty() ? CI : ReplaceInstUsesWith(*CI, With);
1043  }
1044 
1045  return nullptr;
1046 }
Instruction * ReplaceInstUsesWith(Instruction &I, Value *V)
Definition: InstCombine.h:282
LibCallSimplifier * Simplifier
Definition: InstCombine.h:92
Function * getCalledFunction() const
Value * optimizeCall(CallInst *CI)
bool use_empty() const
Definition: Value.h:250
LLVM Value Representation.
Definition: Value.h:69
void Pass::verifyAnalysis ( ) const
virtualinherited

verifyAnalysis() - This member can be implemented by a analysis pass to check state of analysis information.

Reimplemented in llvm::ScalarEvolution, llvm::RegionInfoPass, llvm::LoopInfo, llvm::DominatorTreeWrapperPass, llvm::MachineRegionInfoPass, and llvm::MachineTraceMetrics.

Definition at line 86 of file Pass.cpp.

86  {
87  // By default, don't do anything.
88 }
void llvm::InstVisitor< InstCombiner , Instruction * >::visit ( Iterator  Start,
Iterator  End 
)
inlineinherited

Definition at line 90 of file InstVisitor.h.

90  {
91  while (Start != End)
92  static_cast<SubClass*>(this)->visit(*Start++);
93  }
void visit(Iterator Start, Iterator End)
Definition: InstVisitor.h:90
void llvm::InstVisitor< InstCombiner , Instruction * >::visit ( Module M)
inlineinherited

Definition at line 97 of file InstVisitor.h.

97  {
98  static_cast<SubClass*>(this)->visitModule(M);
99  visit(M.begin(), M.end());
100  }
void visit(Iterator Start, Iterator End)
Definition: InstVisitor.h:90
void llvm::InstVisitor< InstCombiner , Instruction * >::visit ( Function F)
inlineinherited

Definition at line 101 of file InstVisitor.h.

101  {
102  static_cast<SubClass*>(this)->visitFunction(F);
103  visit(F.begin(), F.end());
104  }
F(f)
void visit(Iterator Start, Iterator End)
Definition: InstVisitor.h:90
void llvm::InstVisitor< InstCombiner , Instruction * >::visit ( BasicBlock BB)
inlineinherited

Definition at line 105 of file InstVisitor.h.

105  {
106  static_cast<SubClass*>(this)->visitBasicBlock(BB);
107  visit(BB.begin(), BB.end());
108  }
void visit(Iterator Start, Iterator End)
Definition: InstVisitor.h:90
void llvm::InstVisitor< InstCombiner , Instruction * >::visit ( Module M)
inlineinherited

Definition at line 111 of file InstVisitor.h.

111 { visit(*M); }
void visit(Iterator Start, Iterator End)
Definition: InstVisitor.h:90
void llvm::InstVisitor< InstCombiner , Instruction * >::visit ( Function F)
inlineinherited

Definition at line 112 of file InstVisitor.h.

112 { visit(*F); }
F(f)
void visit(Iterator Start, Iterator End)
Definition: InstVisitor.h:90
void llvm::InstVisitor< InstCombiner , Instruction * >::visit ( BasicBlock BB)
inlineinherited

Definition at line 113 of file InstVisitor.h.

113 { visit(*BB); }
void visit(Iterator Start, Iterator End)
Definition: InstVisitor.h:90
Instruction * llvm::InstVisitor< InstCombiner , Instruction * >::visit ( Instruction I)
inlineinherited

Definition at line 114 of file InstVisitor.h.

114 { return visit(*I); }
void visit(Iterator Start, Iterator End)
Definition: InstVisitor.h:90
#define I(x, y, z)
Definition: MD5.cpp:54
Instruction * llvm::InstVisitor< InstCombiner , Instruction * >::visit ( Instruction I)
inlineinherited

Definition at line 118 of file InstVisitor.h.

118  {
119  switch (I.getOpcode()) {
120  default: llvm_unreachable("Unknown instruction type encountered!");
121  // Build the switch statement using the Instruction.def file...
122 #define HANDLE_INST(NUM, OPCODE, CLASS) \
123  case Instruction::OPCODE: return \
124  static_cast<SubClass*>(this)-> \
125  visit##OPCODE(static_cast<CLASS&>(I));
126 #include "llvm/IR/Instruction.def"
127  }
128  }
#define llvm_unreachable(msg)
Definition: ErrorHandling.h:98
#define I(x, y, z)
Definition: MD5.cpp:54
Instruction * InstCombiner::visitAdd ( BinaryOperator I)

Definition at line 1020 of file InstCombineAddSub.cpp.

1020  {
1021  bool Changed = SimplifyAssociativeOrCommutative(I);
1022  Value *LHS = I.getOperand(0), *RHS = I.getOperand(1);
1023 
1024  if (Value *V = SimplifyVectorOp(I))
1025  return ReplaceInstUsesWith(I, V);
1026 
1027  if (Value *V = SimplifyAddInst(LHS, RHS, I.hasNoSignedWrap(),
1028  I.hasNoUnsignedWrap(), DL))
1029  return ReplaceInstUsesWith(I, V);
1030 
1031  // (A*B)+(A*C) -> A*(B+C) etc
1033  return ReplaceInstUsesWith(I, V);
1034 
1035  if (ConstantInt *CI = dyn_cast<ConstantInt>(RHS)) {
1036  // X + (signbit) --> X ^ signbit
1037  const APInt &Val = CI->getValue();
1038  if (Val.isSignBit())
1039  return BinaryOperator::CreateXor(LHS, RHS);
1040 
1041  // See if SimplifyDemandedBits can simplify this. This handles stuff like
1042  // (X & 254)+1 -> (X&254)|1
1044  return &I;
1045 
1046  // zext(bool) + C -> bool ? C + 1 : C
1047  if (ZExtInst *ZI = dyn_cast<ZExtInst>(LHS))
1048  if (ZI->getSrcTy()->isIntegerTy(1))
1049  return SelectInst::Create(ZI->getOperand(0), AddOne(CI), CI);
1050 
1051  Value *XorLHS = nullptr; ConstantInt *XorRHS = nullptr;
1052  if (match(LHS, m_Xor(m_Value(XorLHS), m_ConstantInt(XorRHS)))) {
1053  uint32_t TySizeBits = I.getType()->getScalarSizeInBits();
1054  const APInt &RHSVal = CI->getValue();
1055  unsigned ExtendAmt = 0;
1056  // If we have ADD(XOR(AND(X, 0xFF), 0x80), 0xF..F80), it's a sext.
1057  // If we have ADD(XOR(AND(X, 0xFF), 0xF..F80), 0x80), it's a sext.
1058  if (XorRHS->getValue() == -RHSVal) {
1059  if (RHSVal.isPowerOf2())
1060  ExtendAmt = TySizeBits - RHSVal.logBase2() - 1;
1061  else if (XorRHS->getValue().isPowerOf2())
1062  ExtendAmt = TySizeBits - XorRHS->getValue().logBase2() - 1;
1063  }
1064 
1065  if (ExtendAmt) {
1066  APInt Mask = APInt::getHighBitsSet(TySizeBits, ExtendAmt);
1067  if (!MaskedValueIsZero(XorLHS, Mask))
1068  ExtendAmt = 0;
1069  }
1070 
1071  if (ExtendAmt) {
1072  Constant *ShAmt = ConstantInt::get(I.getType(), ExtendAmt);
1073  Value *NewShl = Builder->CreateShl(XorLHS, ShAmt, "sext");
1074  return BinaryOperator::CreateAShr(NewShl, ShAmt);
1075  }
1076 
1077  // If this is a xor that was canonicalized from a sub, turn it back into
1078  // a sub and fuse this add with it.
1079  if (LHS->hasOneUse() && (XorRHS->getValue()+1).isPowerOf2()) {
1080  IntegerType *IT = cast<IntegerType>(I.getType());
1081  APInt LHSKnownOne(IT->getBitWidth(), 0);
1082  APInt LHSKnownZero(IT->getBitWidth(), 0);
1083  computeKnownBits(XorLHS, LHSKnownZero, LHSKnownOne);
1084  if ((XorRHS->getValue() | LHSKnownZero).isAllOnesValue())
1085  return BinaryOperator::CreateSub(ConstantExpr::getAdd(XorRHS, CI),
1086  XorLHS);
1087  }
1088  // (X + signbit) + C could have gotten canonicalized to (X ^ signbit) + C,
1089  // transform them into (X + (signbit ^ C))
1090  if (XorRHS->getValue().isSignBit())
1091  return BinaryOperator::CreateAdd(XorLHS,
1092  ConstantExpr::getXor(XorRHS, CI));
1093  }
1094  }
1095 
1096  if (isa<Constant>(RHS) && isa<PHINode>(LHS))
1097  if (Instruction *NV = FoldOpIntoPhi(I))
1098  return NV;
1099 
1100  if (I.getType()->getScalarType()->isIntegerTy(1))
1101  return BinaryOperator::CreateXor(LHS, RHS);
1102 
1103  // X + X --> X << 1
1104  if (LHS == RHS) {
1105  BinaryOperator *New =
1106  BinaryOperator::CreateShl(LHS, ConstantInt::get(I.getType(), 1));
1109  return New;
1110  }
1111 
1112  // -A + B --> B - A
1113  // -A + -B --> -(A + B)
1114  if (Value *LHSV = dyn_castNegVal(LHS)) {
1115  if (!isa<Constant>(RHS))
1116  if (Value *RHSV = dyn_castNegVal(RHS)) {
1117  Value *NewAdd = Builder->CreateAdd(LHSV, RHSV, "sum");
1118  return BinaryOperator::CreateNeg(NewAdd);
1119  }
1120 
1121  return BinaryOperator::CreateSub(RHS, LHSV);
1122  }
1123 
1124  // A + -B --> A - B
1125  if (!isa<Constant>(RHS))
1126  if (Value *V = dyn_castNegVal(RHS))
1127  return BinaryOperator::CreateSub(LHS, V);
1128 
1129  if (Value *V = checkForNegativeOperand(I, Builder))
1130  return ReplaceInstUsesWith(I, V);
1131 
1132  // A+B --> A|B iff A and B have no bits set in common.
1133  if (IntegerType *IT = dyn_cast<IntegerType>(I.getType())) {
1134  APInt LHSKnownOne(IT->getBitWidth(), 0);
1135  APInt LHSKnownZero(IT->getBitWidth(), 0);
1136  computeKnownBits(LHS, LHSKnownZero, LHSKnownOne);
1137  if (LHSKnownZero != 0) {
1138  APInt RHSKnownOne(IT->getBitWidth(), 0);
1139  APInt RHSKnownZero(IT->getBitWidth(), 0);
1140  computeKnownBits(RHS, RHSKnownZero, RHSKnownOne);
1141 
1142  // No bits in common -> bitwise or.
1143  if ((LHSKnownZero|RHSKnownZero).isAllOnesValue())
1144  return BinaryOperator::CreateOr(LHS, RHS);
1145  }
1146  }
1147 
1148  if (Constant *CRHS = dyn_cast<Constant>(RHS)) {
1149  Value *X;
1150  if (match(LHS, m_Not(m_Value(X)))) // ~X + C --> (C-1) - X
1151  return BinaryOperator::CreateSub(SubOne(CRHS), X);
1152  }
1153 
1154  if (ConstantInt *CRHS = dyn_cast<ConstantInt>(RHS)) {
1155  // (X & FF00) + xx00 -> (X+xx00) & FF00
1156  Value *X;
1157  ConstantInt *C2;
1158  if (LHS->hasOneUse() &&
1159  match(LHS, m_And(m_Value(X), m_ConstantInt(C2))) &&
1160  CRHS->getValue() == (CRHS->getValue() & C2->getValue())) {
1161  // See if all bits from the first bit set in the Add RHS up are included
1162  // in the mask. First, get the rightmost bit.
1163  const APInt &AddRHSV = CRHS->getValue();
1164 
1165  // Form a mask of all bits from the lowest bit added through the top.
1166  APInt AddRHSHighBits(~((AddRHSV & -AddRHSV)-1));
1167 
1168  // See if the and mask includes all of these bits.
1169  APInt AddRHSHighBitsAnd(AddRHSHighBits & C2->getValue());
1170 
1171  if (AddRHSHighBits == AddRHSHighBitsAnd) {
1172  // Okay, the xform is safe. Insert the new add pronto.
1173  Value *NewAdd = Builder->CreateAdd(X, CRHS, LHS->getName());
1174  return BinaryOperator::CreateAnd(NewAdd, C2);
1175  }
1176  }
1177 
1178  // Try to fold constant add into select arguments.
1179  if (SelectInst *SI = dyn_cast<SelectInst>(LHS))
1180  if (Instruction *R = FoldOpIntoSelect(I, SI))
1181  return R;
1182  }
1183 
1184  // add (select X 0 (sub n A)) A --> select X A n
1185  {
1186  SelectInst *SI = dyn_cast<SelectInst>(LHS);
1187  Value *A = RHS;
1188  if (!SI) {
1189  SI = dyn_cast<SelectInst>(RHS);
1190  A = LHS;
1191  }
1192  if (SI && SI->hasOneUse()) {
1193  Value *TV = SI->getTrueValue();
1194  Value *FV = SI->getFalseValue();
1195  Value *N;
1196 
1197  // Can we fold the add into the argument of the select?
1198  // We check both true and false select arguments for a matching subtract.
1199  if (match(FV, m_Zero()) && match(TV, m_Sub(m_Value(N), m_Specific(A))))
1200  // Fold the add into the true select value.
1201  return SelectInst::Create(SI->getCondition(), N, A);
1202 
1203  if (match(TV, m_Zero()) && match(FV, m_Sub(m_Value(N), m_Specific(A))))
1204  // Fold the add into the false select value.
1205  return SelectInst::Create(SI->getCondition(), A, N);
1206  }
1207  }
1208 
1209  // Check for (add (sext x), y), see if we can merge this into an
1210  // integer add followed by a sext.
1211  if (SExtInst *LHSConv = dyn_cast<SExtInst>(LHS)) {
1212  // (add (sext x), cst) --> (sext (add x, cst'))
1213  if (ConstantInt *RHSC = dyn_cast<ConstantInt>(RHS)) {
1214  Constant *CI =
1215  ConstantExpr::getTrunc(RHSC, LHSConv->getOperand(0)->getType());
1216  if (LHSConv->hasOneUse() &&
1217  ConstantExpr::getSExt(CI, I.getType()) == RHSC &&
1218  WillNotOverflowSignedAdd(LHSConv->getOperand(0), CI)) {
1219  // Insert the new, smaller add.
1220  Value *NewAdd = Builder->CreateNSWAdd(LHSConv->getOperand(0),
1221  CI, "addconv");
1222  return new SExtInst(NewAdd, I.getType());
1223  }
1224  }
1225 
1226  // (add (sext x), (sext y)) --> (sext (add int x, y))
1227  if (SExtInst *RHSConv = dyn_cast<SExtInst>(RHS)) {
1228  // Only do this if x/y have the same type, if at last one of them has a
1229  // single use (so we don't increase the number of sexts), and if the
1230  // integer add will not overflow.
1231  if (LHSConv->getOperand(0)->getType()==RHSConv->getOperand(0)->getType()&&
1232  (LHSConv->hasOneUse() || RHSConv->hasOneUse()) &&
1233  WillNotOverflowSignedAdd(LHSConv->getOperand(0),
1234  RHSConv->getOperand(0))) {
1235  // Insert the new integer add.
1236  Value *NewAdd = Builder->CreateNSWAdd(LHSConv->getOperand(0),
1237  RHSConv->getOperand(0), "addconv");
1238  return new SExtInst(NewAdd, I.getType());
1239  }
1240  }
1241  }
1242 
1243  // Check for (x & y) + (x ^ y)
1244  {
1245  Value *A = nullptr, *B = nullptr;
1246  if (match(RHS, m_Xor(m_Value(A), m_Value(B))) &&
1247  (match(LHS, m_And(m_Specific(A), m_Specific(B))) ||
1248  match(LHS, m_And(m_Specific(B), m_Specific(A)))))
1249  return BinaryOperator::CreateOr(A, B);
1250 
1251  if (match(LHS, m_Xor(m_Value(A), m_Value(B))) &&
1252  (match(RHS, m_And(m_Specific(A), m_Specific(B))) ||
1253  match(RHS, m_And(m_Specific(B), m_Specific(A)))))
1254  return BinaryOperator::CreateOr(A, B);
1255  }
1256 
1257  // TODO(jingyue): Consider WillNotOverflowSignedAdd and
1258  // WillNotOverflowUnsignedAdd to reduce the number of invocations of
1259  // computeKnownBits.
1260  if (!I.hasNoSignedWrap() && WillNotOverflowSignedAdd(LHS, RHS)) {
1261  Changed = true;
1262  I.setHasNoSignedWrap(true);
1263  }
1264  if (!I.hasNoUnsignedWrap() && WillNotOverflowUnsignedAdd(LHS, RHS)) {
1265  Changed = true;
1266  I.setHasNoUnsignedWrap(true);
1267  }
1268 
1269  return Changed ? &I : nullptr;
1270 }
Instruction * FoldOpIntoPhi(Instruction &I)
BinaryOp_match< LHS, RHS, Instruction::And > m_And(const LHS &L, const RHS &R)
Definition: PatternMatch.h:467
class_match< Value > m_Value()
m_Value() - Match an arbitrary value and ignore it.
Definition: PatternMatch.h:70
void setHasNoSignedWrap(bool b=true)
BinaryOp_match< LHS, RHS, Instruction::Sub > m_Sub(const LHS &L, const RHS &R)
Definition: PatternMatch.h:407
bool WillNotOverflowSignedAdd(Value *LHS, Value *RHS)
match_zero m_Zero()
Definition: PatternMatch.h:137
This class represents zero extension of integer types.
Value * SimplifyVectorOp(BinaryOperator &Inst)
Makes transformation of binary operation specific for vector types.
static RegisterPass< NVPTXAllocaHoisting > X("alloca-hoisting","Hoisting alloca instructions in non-entry ""blocks to the entry block")
Instruction * FoldOpIntoSelect(Instruction &Op, SelectInst *SI)
Value * SimplifyAddInst(Value *LHS, Value *RHS, bool isNSW, bool isNUW, const DataLayout *TD=nullptr, const TargetLibraryInfo *TLI=nullptr, const DominatorTree *DT=nullptr)
This class represents a sign extension of integer types.
unsigned getBitWidth() const
Get the number of bits in this IntegerType.
Definition: DerivedTypes.h:61
StringRef getName() const
Definition: Value.cpp:168
static Constant * getAdd(Constant *C1, Constant *C2, bool HasNUW=false, bool HasNSW=false)
Definition: Constants.cpp:2102
Value * CreateNSWAdd(Value *LHS, Value *RHS, const Twine &Name="")
Definition: IRBuilder.h:640
bool match(Val *V, const Pattern &P)
Definition: PatternMatch.h:42
BinaryOp_match< LHS, RHS, Instruction::Xor > m_Xor(const LHS &L, const RHS &R)
Definition: PatternMatch.h:479
static cl::opt< ITMode > IT(cl::desc("IT block support"), cl::Hidden, cl::init(DefaultIT), cl::ZeroOrMore, cl::values(clEnumValN(DefaultIT,"arm-default-it","Generate IT block based on arch"), clEnumValN(RestrictedIT,"arm-restrict-it","Disallow deprecated IT based on ARMv8"), clEnumValN(NoRestrictedIT,"arm-no-restrict-it","Allow IT blocks based on ARMv7"), clEnumValEnd))
const APInt & getValue() const
Return the constant's value.
Definition: Constants.h:105
void setHasNoUnsignedWrap(bool b=true)
bool WillNotOverflowUnsignedAdd(Value *LHS, Value *RHS)
static Constant * AddOne(Constant *C)
AddOne - Add one to a Constant.
Definition: InstCombine.h:60
not_match< LHS > m_Not(const LHS &L)
Definition: PatternMatch.h:828
static SelectInst * Create(Value *C, Value *S1, Value *S2, const Twine &NameStr="", Instruction *InsertBefore=nullptr)
Value * SimplifyUsingDistributiveLaws(BinaryOperator &I)
class_match< ConstantInt > m_ConstantInt()
m_ConstantInt() - Match an arbitrary ConstantInt and ignore it.
Definition: PatternMatch.h:72
static APInt getHighBitsSet(unsigned numBits, unsigned hiBitsSet)
Get a value with high bits set.
Definition: APInt.h:508
Value * dyn_castNegVal(Value *V) const
Value * CreateAdd(Value *LHS, Value *RHS, const Twine &Name="", bool HasNUW=false, bool HasNSW=false)
Definition: IRBuilder.h:632
LLVM Constant Representation.
Definition: Constant.h:41
const Value * getCondition() const
Instruction * ReplaceInstUsesWith(Instruction &I, Value *V)
Definition: InstCombine.h:282
specificval_ty m_Specific(const Value *V)
m_Specific - Match if we have a specific specified value.
Definition: PatternMatch.h:323
Value * getOperand(unsigned i) const
Definition: User.h:90
Integer representation type.
Definition: DerivedTypes.h:37
BuilderTy * Builder
Definition: InstCombine.h:102
NUW NUW NUW NUW Exact static Exact BinaryOperator * CreateNeg(Value *Op, const Twine &Name="", Instruction *InsertBefore=nullptr)
bool hasNoSignedWrap() const
hasNoSignedWrap - Determine whether the no signed wrap flag is set.
const Value * getTrueValue() const
bool isPowerOf2() const
Check if this APInt's value is a power of two greater than zero.
Definition: APInt.h:388
Definition: test.h:1
bool SimplifyAssociativeOrCommutative(BinaryOperator &I)
Class for constant integers.
Definition: Constants.h:51
unsigned getScalarSizeInBits() const LLVM_READONLY
Definition: Type.cpp:135
unsigned logBase2() const
Definition: APInt.h:1496
Type * getType() const
Definition: Value.h:215
static Constant * get(Type *Ty, uint64_t V, bool isSigned=false)
Definition: Constants.cpp:528
static Constant * getTrunc(Constant *C, Type *Ty)
Definition: Constants.cpp:1587
Class for arbitrary precision integers.
Definition: APInt.h:75
bool isIntegerTy() const
Definition: Type.h:193
LLVM_ATTRIBUTE_UNUSED_RESULT std::enable_if< !is_simple_type< Y >::value, typename cast_retty< X, const Y >::ret_type >::type dyn_cast(const Y &Val)
Definition: Casting.h:265
const Type * getScalarType() const LLVM_READONLY
Definition: Type.cpp:51
Value * CreateShl(Value *LHS, Value *RHS, const Twine &Name="", bool HasNUW=false, bool HasNSW=false)
Definition: IRBuilder.h:751
static Constant * getSExt(Constant *C, Type *Ty)
Definition: Constants.cpp:1601
#define N
bool hasOneUse() const
Definition: Value.h:284
bool isSignBit() const
Check if the APInt's value is returned by getSignBit.
Definition: APInt.h:397
bool MaskedValueIsZero(Value *V, const APInt &Mask, unsigned Depth=0) const
Definition: InstCombine.h:323
const DataLayout * DL
Definition: InstCombine.h:89
LLVM Value Representation.
Definition: Value.h:69
bool hasNoUnsignedWrap() const
hasNoUnsignedWrap - Determine whether the no unsigned wrap flag is set.
const Value * getFalseValue() const
void computeKnownBits(Value *V, APInt &KnownZero, APInt &KnownOne, unsigned Depth=0) const
Definition: InstCombine.h:318
bool SimplifyDemandedInstructionBits(Instruction &Inst)
static Value * checkForNegativeOperand(BinaryOperator &I, InstCombiner::BuilderTy *Builder)
static Constant * SubOne(Constant *C)
SubOne - Subtract one from a Constant.
Definition: InstCombine.h:64
static Constant * getXor(Constant *C1, Constant *C2)
Definition: Constants.cpp:2169
Instruction * InstCombiner::visitAddrSpaceCast ( AddrSpaceCastInst CI)

Definition at line 1911 of file InstCombineCasts.cpp.

1911  {
1912  // If the destination pointer element type is not the same as the source's
1913  // first do a bitcast to the destination type, and then the addrspacecast.
1914  // This allows the cast to be exposed to other transforms.
1915  Value *Src = CI.getOperand(0);
1916  PointerType *SrcTy = cast<PointerType>(Src->getType()->getScalarType());
1917  PointerType *DestTy = cast<PointerType>(CI.getType()->getScalarType());
1918 
1919  Type *DestElemTy = DestTy->getElementType();
1920  if (SrcTy->getElementType() != DestElemTy) {
1921  Type *MidTy = PointerType::get(DestElemTy, SrcTy->getAddressSpace());
1922  if (VectorType *VT = dyn_cast<VectorType>(CI.getType())) {
1923  // Handle vectors of pointers.
1924  MidTy = VectorType::get(MidTy, VT->getNumElements());
1925  }
1926 
1927  Value *NewBitCast = Builder->CreateBitCast(Src, MidTy);
1928  return new AddrSpaceCastInst(NewBitCast, CI.getType());
1929  }
1930 
1931  return commonPointerCastTransforms(CI);
1932 }
static PointerType * get(Type *ElementType, unsigned AddressSpace)
Definition: Type.cpp:727
unsigned getAddressSpace() const
Return the address space of the Pointer type.
Definition: DerivedTypes.h:465
This class represents a conversion between pointers from one address space to another.
Type * getElementType() const
Definition: DerivedTypes.h:319
Value * getOperand(unsigned i) const
Definition: User.h:90
BuilderTy * Builder
Definition: InstCombine.h:102
Value * CreateBitCast(Value *V, Type *DestTy, const Twine &Name="")
Definition: IRBuilder.h:1158
Type * getType() const
Definition: Value.h:215
const Type * getScalarType() const LLVM_READONLY
Definition: Type.cpp:51
LLVM Value Representation.
Definition: Value.h:69
static VectorType * get(Type *ElementType, unsigned NumElements)
Definition: Type.cpp:703
Instruction * commonPointerCastTransforms(CastInst &CI)
Implement the transforms for cast of pointer (bitcast/ptrtoint)
Instruction * llvm::InstVisitor< InstCombiner , Instruction * >::visitAddrSpaceCastInst ( AddrSpaceCastInst I)
inlineinherited

Definition at line 194 of file InstVisitor.h.

194 { DELEGATE(CastInst);}
#define DELEGATE(CLASS_TO_VISIT)
Definition: InstVisitor.h:30
Instruction * InstCombiner::visitAllocaInst ( AllocaInst AI)

Definition at line 165 of file InstCombineLoadStoreAlloca.cpp.

165  {
166  // Ensure that the alloca array size argument has type intptr_t, so that
167  // any casting is exposed early.
168  if (DL) {
169  Type *IntPtrTy = DL->getIntPtrType(AI.getType());
170  if (AI.getArraySize()->getType() != IntPtrTy) {
172  IntPtrTy, false);
173  AI.setOperand(0, V);
174  return &AI;
175  }
176  }
177 
178  // Convert: alloca Ty, C - where C is a constant != 1 into: alloca [C x Ty], 1
179  if (AI.isArrayAllocation()) { // Check C != 1
180  if (const ConstantInt *C = dyn_cast<ConstantInt>(AI.getArraySize())) {
181  Type *NewTy =
182  ArrayType::get(AI.getAllocatedType(), C->getZExtValue());
183  AllocaInst *New = Builder->CreateAlloca(NewTy, nullptr, AI.getName());
184  New->setAlignment(AI.getAlignment());
185 
186  // Scan to the end of the allocation instructions, to skip over a block of
187  // allocas if possible...also skip interleaved debug info
188  //
189  BasicBlock::iterator It = New;
190  while (isa<AllocaInst>(*It) || isa<DbgInfoIntrinsic>(*It)) ++It;
191 
192  // Now that I is pointing to the first non-allocation-inst in the block,
193  // insert our getelementptr instruction...
194  //
195  Type *IdxTy = DL
196  ? DL->getIntPtrType(AI.getType())
198  Value *NullIdx = Constant::getNullValue(IdxTy);
199  Value *Idx[2] = { NullIdx, NullIdx };
200  Instruction *GEP =
201  GetElementPtrInst::CreateInBounds(New, Idx, New->getName() + ".sub");
202  InsertNewInstBefore(GEP, *It);
203 
204  // Now make everything use the getelementptr instead of the original
205  // allocation.
206  return ReplaceInstUsesWith(AI, GEP);
207  } else if (isa<UndefValue>(AI.getArraySize())) {
209  }
210  }
211 
212  if (DL && AI.getAllocatedType()->isSized()) {
213  // If the alignment is 0 (unspecified), assign it the preferred alignment.
214  if (AI.getAlignment() == 0)
216 
217  // Move all alloca's of zero byte objects to the entry block and merge them
218  // together. Note that we only do this for alloca's, because malloc should
219  // allocate and return a unique pointer, even for a zero byte allocation.
220  if (DL->getTypeAllocSize(AI.getAllocatedType()) == 0) {
221  // For a zero sized alloca there is no point in doing an array allocation.
222  // This is helpful if the array size is a complicated expression not used
223  // elsewhere.
224  if (AI.isArrayAllocation()) {
225  AI.setOperand(0, ConstantInt::get(AI.getArraySize()->getType(), 1));
226  return &AI;
227  }
228 
229  // Get the first instruction in the entry block.
230  BasicBlock &EntryBlock = AI.getParent()->getParent()->getEntryBlock();
231  Instruction *FirstInst = EntryBlock.getFirstNonPHIOrDbg();
232  if (FirstInst != &AI) {
233  // If the entry block doesn't start with a zero-size alloca then move
234  // this one to the start of the entry block. There is no problem with
235  // dominance as the array size was forced to a constant earlier already.
236  AllocaInst *EntryAI = dyn_cast<AllocaInst>(FirstInst);
237  if (!EntryAI || !EntryAI->getAllocatedType()->isSized() ||
238  DL->getTypeAllocSize(EntryAI->getAllocatedType()) != 0) {
239  AI.moveBefore(FirstInst);
240  return &AI;
241  }
242 
243  // If the alignment of the entry block alloca is 0 (unspecified),
244  // assign it the preferred alignment.
245  if (EntryAI->getAlignment() == 0)
246  EntryAI->setAlignment(
248  // Replace this zero-sized alloca with the one at the start of the entry
249  // block after ensuring that the address will be aligned enough for both
250  // types.
251  unsigned MaxAlign = std::max(EntryAI->getAlignment(),
252  AI.getAlignment());
253  EntryAI->setAlignment(MaxAlign);
254  if (AI.getType() != EntryAI->getType())
255  return new BitCastInst(EntryAI, AI.getType());
256  return ReplaceInstUsesWith(AI, EntryAI);
257  }
258  }
259  }
260 
261  if (AI.getAlignment()) {
262  // Check to see if this allocation is only modified by a memcpy/memmove from
263  // a constant global whose alignment is equal to or exceeds that of the
264  // allocation. If this is the case, we can change all users to use
265  // the constant global instead. This is commonly produced by the CFE by
266  // constructs like "void foo() { int A[] = {1,2,3,4,5,6,7,8,9...}; }" if 'A'
267  // is only subsequently read.
269  if (MemTransferInst *Copy = isOnlyCopiedFromConstantGlobal(&AI, ToDelete)) {
270  unsigned SourceAlign = getOrEnforceKnownAlignment(Copy->getSource(),
271  AI.getAlignment(), DL);
272  if (AI.getAlignment() <= SourceAlign) {
273  DEBUG(dbgs() << "Found alloca equal to global: " << AI << '\n');
274  DEBUG(dbgs() << " memcpy = " << *Copy << '\n');
275  for (unsigned i = 0, e = ToDelete.size(); i != e; ++i)
276  EraseInstFromFunction(*ToDelete[i]);
277  Constant *TheSrc = cast<Constant>(Copy->getSource());
278  Constant *Cast
280  Instruction *NewI = ReplaceInstUsesWith(AI, Cast);
281  EraseInstFromFunction(*Copy);
282  ++NumGlobalCopies;
283  return NewI;
284  }
285  }
286  }
287 
288  // At last, use the generic allocation site handler to aggressively remove
289  // unused allocas.
290  return visitAllocSite(AI);
291 }
AllocaInst * CreateAlloca(Type *Ty, Value *ArraySize=nullptr, const Twine &Name="")
Definition: IRBuilder.h:895
static Constant * getPointerBitCastOrAddrSpaceCast(Constant *C, Type *Ty)
Create a BitCast or AddrSpaceCast for a pointer type depending on the address space.
Definition: Constants.cpp:1551
Value * CreateIntCast(Value *V, Type *DestTy, bool isSigned, const Twine &Name="")
Definition: IRBuilder.h:1221
void setAlignment(unsigned Align)
unsigned getOrEnforceKnownAlignment(Value *V, unsigned PrefAlign, const DataLayout *TD=nullptr)
unsigned getPrefTypeAlignment(Type *Ty) const
Definition: DataLayout.cpp:693
const Function * getParent() const
Return the enclosing method, or null if none.
Definition: BasicBlock.h:118
static IntegerType * getInt64Ty(LLVMContext &C)
Definition: Type.cpp:236
static bool isOnlyCopiedFromConstantGlobal(Value *V, MemTransferInst *&TheCopy, SmallVectorImpl< Instruction * > &ToDelete)
static Constant * getNullValue(Type *Ty)
Definition: Constants.cpp:133
StringRef getName() const
Definition: Value.cpp:168
bool isArrayAllocation() const
Instruction * getFirstNonPHIOrDbg()
Returns a pointer to the first instruction in this block that is not a PHINode or a debug intrinsic...
Definition: BasicBlock.cpp:144
Type * getAllocatedType() const
static GetElementPtrInst * CreateInBounds(Value *Ptr, ArrayRef< Value * > IdxList, const Twine &NameStr="", Instruction *InsertBefore=nullptr)
Definition: Instructions.h:809
This class represents a no-op cast from one type to another.
LLVM Basic Block Representation.
Definition: BasicBlock.h:72
LLVM Constant Representation.
Definition: Constant.h:41
PointerType * getType() const
Definition: Instructions.h:93
Instruction * ReplaceInstUsesWith(Instruction &I, Value *V)
Definition: InstCombine.h:282
unsigned getAlignment() const
Definition: Instructions.h:105
#define DEBUG(X)
BuilderTy * Builder
Definition: InstCombine.h:102
LLVMContext & getContext() const
All values hold a context through their type.
Definition: Value.cpp:615
IntegerType * getIntPtrType(LLVMContext &C, unsigned AddressSpace=0) const
Definition: DataLayout.cpp:703
Class for constant integers.
Definition: Constants.h:51
uint64_t getTypeAllocSize(Type *Ty) const
Definition: DataLayout.h:368
Type * getType() const
Definition: Value.h:215
static Constant * get(Type *Ty, uint64_t V, bool isSigned=false)
Definition: Constants.cpp:528
const BasicBlock & getEntryBlock() const
Definition: Function.h:387
void setOperand(unsigned i, Value *Val)
Definition: User.h:94
raw_ostream & dbgs()
dbgs - Return a circular-buffered debug stream.
bool isSized(SmallPtrSet< const Type *, 4 > *Visited=nullptr) const
Definition: Type.h:268
LLVM_ATTRIBUTE_UNUSED_RESULT std::enable_if< !is_simple_type< Y >::value, typename cast_retty< X, const Y >::ret_type >::type dyn_cast(const Y &Val)
Definition: Casting.h:265
static ArrayType * get(Type *ElementType, uint64_t NumElements)
Definition: Type.cpp:676
Instruction * InsertNewInstBefore(Instruction *New, Instruction &Old)
Definition: InstCombine.h:259
const DataLayout * DL
Definition: InstCombine.h:89
LLVM Value Representation.
Definition: Value.h:69
const Value * getArraySize() const
Definition: Instructions.h:88
Instruction * EraseInstFromFunction(Instruction &I)
Definition: InstCombine.h:301
void moveBefore(Instruction *MovePos)
Definition: Instruction.cpp:95
Instruction * visitAllocSite(Instruction &FI)
const BasicBlock * getParent() const
Definition: Instruction.h:53
Instruction * InstCombiner::visitAllocSite ( Instruction FI)

Definition at line 1800 of file InstructionCombining.cpp.

1800  {
1801  // If we have a malloc call which is only used in any amount of comparisons
1802  // to null and free calls, delete the calls and replace the comparisons with
1803  // true or false as appropriate.
1805  if (isAllocSiteRemovable(&MI, Users, TLI)) {
1806  for (unsigned i = 0, e = Users.size(); i != e; ++i) {
1807  Instruction *I = cast_or_null<Instruction>(&*Users[i]);
1808  if (!I) continue;
1809 
1810  if (ICmpInst *C = dyn_cast<ICmpInst>(I)) {
1812  ConstantInt::get(Type::getInt1Ty(C->getContext()),
1813  C->isFalseWhenEqual()));
1814  } else if (isa<BitCastInst>(I) || isa<GetElementPtrInst>(I)) {
1816  } else if (IntrinsicInst *II = dyn_cast<IntrinsicInst>(I)) {
1817  if (II->getIntrinsicID() == Intrinsic::objectsize) {
1818  ConstantInt *CI = cast<ConstantInt>(II->getArgOperand(1));
1819  uint64_t DontKnow = CI->isZero() ? -1ULL : 0;
1820  ReplaceInstUsesWith(*I, ConstantInt::get(I->getType(), DontKnow));
1821  }
1822  }
1824  }
1825 
1826  if (InvokeInst *II = dyn_cast<InvokeInst>(&MI)) {
1827  // Replace invoke with a NOP intrinsic to maintain the original CFG
1828  Module *M = II->getParent()->getParent()->getParent();
1829  Function *F = Intrinsic::getDeclaration(M, Intrinsic::donothing);
1830  InvokeInst::Create(F, II->getNormalDest(), II->getUnwindDest(),
1831  None, "", II->getParent());
1832  }
1833  return EraseInstFromFunction(MI);
1834  }
1835  return nullptr;
1836 }
static IntegerType * getInt1Ty(LLVMContext &C)
Definition: Type.cpp:232
The main container class for the LLVM Intermediate Representation.
Definition: Module.h:115
F(f)
iv Induction Variable Users
Definition: IVUsers.cpp:39
TargetLibraryInfo * TLI
Definition: InstCombine.h:90
Function * getDeclaration(Module *M, ID id, ArrayRef< Type * > Tys=None)
Definition: Function.cpp:737
Instruction * ReplaceInstUsesWith(Instruction &I, Value *V)
Definition: InstCombine.h:282
Represent an integer comparison operator.
Definition: Instructions.h:977
static UndefValue * get(Type *T)
Definition: Constants.cpp:1372
static InvokeInst * Create(Value *Func, BasicBlock *IfNormal, BasicBlock *IfException, ArrayRef< Value * > Args, const Twine &NameStr="", Instruction *InsertBefore=nullptr)
Class for constant integers.
Definition: Constants.h:51
Type * getType() const
Definition: Value.h:215
static Constant * get(Type *Ty, uint64_t V, bool isSigned=false)
Definition: Constants.cpp:528
bool isZero() const
Definition: Constants.h:160
#define I(x, y, z)
Definition: MD5.cpp:54
static bool isAllocSiteRemovable(Instruction *AI, SmallVectorImpl< WeakVH > &Users, const TargetLibraryInfo *TLI)
Instruction * EraseInstFromFunction(Instruction &I)
Definition: InstCombine.h:301
Instruction * InstCombiner::visitAnd ( BinaryOperator I)

Definition at line 1105 of file InstCombineAndOrXor.cpp.

1105  {
1106  bool Changed = SimplifyAssociativeOrCommutative(I);
1107  Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
1108 
1109  if (Value *V = SimplifyVectorOp(I))
1110  return ReplaceInstUsesWith(I, V);
1111 
1112  if (Value *V = SimplifyAndInst(Op0, Op1, DL))
1113  return ReplaceInstUsesWith(I, V);
1114 
1115  // (A|B)&(A|C) -> A|(B&C) etc
1117  return ReplaceInstUsesWith(I, V);
1118 
1119  // See if we can simplify any instructions used by the instruction whose sole
1120  // purpose is to compute bits we don't care about.
1122  return &I;
1123 
1124  if (ConstantInt *AndRHS = dyn_cast<ConstantInt>(Op1)) {
1125  const APInt &AndRHSMask = AndRHS->getValue();
1126 
1127  // Optimize a variety of ((val OP C1) & C2) combinations...
1128  if (BinaryOperator *Op0I = dyn_cast<BinaryOperator>(Op0)) {
1129  Value *Op0LHS = Op0I->getOperand(0);
1130  Value *Op0RHS = Op0I->getOperand(1);
1131  switch (Op0I->getOpcode()) {
1132  default: break;
1133  case Instruction::Xor:
1134  case Instruction::Or: {
1135  // If the mask is only needed on one incoming arm, push it up.
1136  if (!Op0I->hasOneUse()) break;
1137 
1138  APInt NotAndRHS(~AndRHSMask);
1139  if (MaskedValueIsZero(Op0LHS, NotAndRHS)) {
1140  // Not masking anything out for the LHS, move to RHS.
1141  Value *NewRHS = Builder->CreateAnd(Op0RHS, AndRHS,
1142  Op0RHS->getName()+".masked");
1143  return BinaryOperator::Create(Op0I->getOpcode(), Op0LHS, NewRHS);
1144  }
1145  if (!isa<Constant>(Op0RHS) &&
1146  MaskedValueIsZero(Op0RHS, NotAndRHS)) {
1147  // Not masking anything out for the RHS, move to LHS.
1148  Value *NewLHS = Builder->CreateAnd(Op0LHS, AndRHS,
1149  Op0LHS->getName()+".masked");
1150  return BinaryOperator::Create(Op0I->getOpcode(), NewLHS, Op0RHS);
1151  }
1152 
1153  break;
1154  }
1155  case Instruction::Add:
1156  // ((A & N) + B) & AndRHS -> (A + B) & AndRHS iff N&AndRHS == AndRHS.
1157  // ((A | N) + B) & AndRHS -> (A + B) & AndRHS iff N&AndRHS == 0
1158  // ((A ^ N) + B) & AndRHS -> (A + B) & AndRHS iff N&AndRHS == 0
1159  if (Value *V = FoldLogicalPlusAnd(Op0LHS, Op0RHS, AndRHS, false, I))
1160  return BinaryOperator::CreateAnd(V, AndRHS);
1161  if (Value *V = FoldLogicalPlusAnd(Op0RHS, Op0LHS, AndRHS, false, I))
1162  return BinaryOperator::CreateAnd(V, AndRHS); // Add commutes
1163  break;
1164 
1165  case Instruction::Sub:
1166  // ((A & N) - B) & AndRHS -> (A - B) & AndRHS iff N&AndRHS == AndRHS.
1167  // ((A | N) - B) & AndRHS -> (A - B) & AndRHS iff N&AndRHS == 0
1168  // ((A ^ N) - B) & AndRHS -> (A - B) & AndRHS iff N&AndRHS == 0
1169  if (Value *V = FoldLogicalPlusAnd(Op0LHS, Op0RHS, AndRHS, true, I))
1170  return BinaryOperator::CreateAnd(V, AndRHS);
1171 
1172  // (A - N) & AndRHS -> -N & AndRHS iff A&AndRHS==0 and AndRHS
1173  // has 1's for all bits that the subtraction with A might affect.
1174  if (Op0I->hasOneUse() && !match(Op0LHS, m_Zero())) {
1175  uint32_t BitWidth = AndRHSMask.getBitWidth();
1176  uint32_t Zeros = AndRHSMask.countLeadingZeros();
1177  APInt Mask = APInt::getLowBitsSet(BitWidth, BitWidth - Zeros);
1178 
1179  if (MaskedValueIsZero(Op0LHS, Mask)) {
1180  Value *NewNeg = Builder->CreateNeg(Op0RHS);
1181  return BinaryOperator::CreateAnd(NewNeg, AndRHS);
1182  }
1183  }
1184  break;
1185 
1186  case Instruction::Shl:
1187  case Instruction::LShr:
1188  // (1 << x) & 1 --> zext(x == 0)
1189  // (1 >> x) & 1 --> zext(x == 0)
1190  if (AndRHSMask == 1 && Op0LHS == AndRHS) {
1191  Value *NewICmp =
1193  return new ZExtInst(NewICmp, I.getType());
1194  }
1195  break;
1196  }
1197 
1198  if (ConstantInt *Op0CI = dyn_cast<ConstantInt>(Op0I->getOperand(1)))
1199  if (Instruction *Res = OptAndOp(Op0I, Op0CI, AndRHS, I))
1200  return Res;
1201  }
1202 
1203  // If this is an integer truncation, and if the source is an 'and' with
1204  // immediate, transform it. This frequently occurs for bitfield accesses.
1205  {
1206  Value *X = nullptr; ConstantInt *YC = nullptr;
1207  if (match(Op0, m_Trunc(m_And(m_Value(X), m_ConstantInt(YC))))) {
1208  // Change: and (trunc (and X, YC) to T), C2
1209  // into : and (trunc X to T), trunc(YC) & C2
1210  // This will fold the two constants together, which may allow
1211  // other simplifications.
1212  Value *NewCast = Builder->CreateTrunc(X, I.getType(), "and.shrunk");
1213  Constant *C3 = ConstantExpr::getTrunc(YC, I.getType());
1214  C3 = ConstantExpr::getAnd(C3, AndRHS);
1215  return BinaryOperator::CreateAnd(NewCast, C3);
1216  }
1217  }
1218 
1219  // Try to fold constant and into select arguments.
1220  if (SelectInst *SI = dyn_cast<SelectInst>(Op0))
1221  if (Instruction *R = FoldOpIntoSelect(I, SI))
1222  return R;
1223  if (isa<PHINode>(Op0))
1224  if (Instruction *NV = FoldOpIntoPhi(I))
1225  return NV;
1226  }
1227 
1228 
1229  // (~A & ~B) == (~(A | B)) - De Morgan's Law
1230  if (Value *Op0NotVal = dyn_castNotVal(Op0))
1231  if (Value *Op1NotVal = dyn_castNotVal(Op1))
1232  if (Op0->hasOneUse() && Op1->hasOneUse()) {
1233  Value *Or = Builder->CreateOr(Op0NotVal, Op1NotVal,
1234  I.getName()+".demorgan");
1235  return BinaryOperator::CreateNot(Or);
1236  }
1237 
1238  {
1239  Value *A = nullptr, *B = nullptr, *C = nullptr, *D = nullptr;
1240  // (A|B) & ~(A&B) -> A^B
1241  if (match(Op0, m_Or(m_Value(A), m_Value(B))) &&
1242  match(Op1, m_Not(m_And(m_Value(C), m_Value(D)))) &&
1243  ((A == C && B == D) || (A == D && B == C)))
1244  return BinaryOperator::CreateXor(A, B);
1245 
1246  // ~(A&B) & (A|B) -> A^B
1247  if (match(Op1, m_Or(m_Value(A), m_Value(B))) &&
1248  match(Op0, m_Not(m_And(m_Value(C), m_Value(D)))) &&
1249  ((A == C && B == D) || (A == D && B == C)))
1250  return BinaryOperator::CreateXor(A, B);
1251 
1252  // A&(A^B) => A & ~B
1253  {
1254  Value *tmpOp0 = Op0;
1255  Value *tmpOp1 = Op1;
1256  if (Op0->hasOneUse() &&
1257  match(Op0, m_Xor(m_Value(A), m_Value(B)))) {
1258  if (A == Op1 || B == Op1 ) {
1259  tmpOp1 = Op0;
1260  tmpOp0 = Op1;
1261  // Simplify below
1262  }
1263  }
1264 
1265  if (tmpOp1->hasOneUse() &&
1266  match(tmpOp1, m_Xor(m_Value(A), m_Value(B)))) {
1267  if (B == tmpOp0) {
1268  std::swap(A, B);
1269  }
1270  // Notice that the patten (A&(~B)) is actually (A&(-1^B)), so if
1271  // A is originally -1 (or a vector of -1 and undefs), then we enter
1272  // an endless loop. By checking that A is non-constant we ensure that
1273  // we will never get to the loop.
1274  if (A == tmpOp0 && !isa<Constant>(A)) // A&(A^B) -> A & ~B
1275  return BinaryOperator::CreateAnd(A, Builder->CreateNot(B));
1276  }
1277  }
1278 
1279  // (A&((~A)|B)) -> A&B
1280  if (match(Op0, m_Or(m_Not(m_Specific(Op1)), m_Value(A))) ||
1281  match(Op0, m_Or(m_Value(A), m_Not(m_Specific(Op1)))))
1282  return BinaryOperator::CreateAnd(A, Op1);
1283  if (match(Op1, m_Or(m_Not(m_Specific(Op0)), m_Value(A))) ||
1284  match(Op1, m_Or(m_Value(A), m_Not(m_Specific(Op0)))))
1285  return BinaryOperator::CreateAnd(A, Op0);
1286  }
1287 
1288  if (ICmpInst *RHS = dyn_cast<ICmpInst>(Op1))
1289  if (ICmpInst *LHS = dyn_cast<ICmpInst>(Op0))
1290  if (Value *Res = FoldAndOfICmps(LHS, RHS))
1291  return ReplaceInstUsesWith(I, Res);
1292 
1293  // If and'ing two fcmp, try combine them into one.
1294  if (FCmpInst *LHS = dyn_cast<FCmpInst>(I.getOperand(0)))
1295  if (FCmpInst *RHS = dyn_cast<FCmpInst>(I.getOperand(1)))
1296  if (Value *Res = FoldAndOfFCmps(LHS, RHS))
1297  return ReplaceInstUsesWith(I, Res);
1298 
1299 
1300  // fold (and (cast A), (cast B)) -> (cast (and A, B))
1301  if (CastInst *Op0C = dyn_cast<CastInst>(Op0))
1302  if (CastInst *Op1C = dyn_cast<CastInst>(Op1)) {
1303  Type *SrcTy = Op0C->getOperand(0)->getType();
1304  if (Op0C->getOpcode() == Op1C->getOpcode() && // same cast kind ?
1305  SrcTy == Op1C->getOperand(0)->getType() &&
1306  SrcTy->isIntOrIntVectorTy()) {
1307  Value *Op0COp = Op0C->getOperand(0), *Op1COp = Op1C->getOperand(0);
1308 
1309  // Only do this if the casts both really cause code to be generated.
1310  if (ShouldOptimizeCast(Op0C->getOpcode(), Op0COp, I.getType()) &&
1311  ShouldOptimizeCast(Op1C->getOpcode(), Op1COp, I.getType())) {
1312  Value *NewOp = Builder->CreateAnd(Op0COp, Op1COp, I.getName());
1313  return CastInst::Create(Op0C->getOpcode(), NewOp, I.getType());
1314  }
1315 
1316  // If this is and(cast(icmp), cast(icmp)), try to fold this even if the
1317  // cast is otherwise not optimizable. This happens for vector sexts.
1318  if (ICmpInst *RHS = dyn_cast<ICmpInst>(Op1COp))
1319  if (ICmpInst *LHS = dyn_cast<ICmpInst>(Op0COp))
1320  if (Value *Res = FoldAndOfICmps(LHS, RHS))
1321  return CastInst::Create(Op0C->getOpcode(), Res, I.getType());
1322 
1323  // If this is and(cast(fcmp), cast(fcmp)), try to fold this even if the
1324  // cast is otherwise not optimizable. This happens for vector sexts.
1325  if (FCmpInst *RHS = dyn_cast<FCmpInst>(Op1COp))
1326  if (FCmpInst *LHS = dyn_cast<FCmpInst>(Op0COp))
1327  if (Value *Res = FoldAndOfFCmps(LHS, RHS))
1328  return CastInst::Create(Op0C->getOpcode(), Res, I.getType());
1329  }
1330  }
1331 
1332  // (X >> Z) & (Y >> Z) -> (X&Y) >> Z for all shifts.
1333  if (BinaryOperator *SI1 = dyn_cast<BinaryOperator>(Op1)) {
1334  if (BinaryOperator *SI0 = dyn_cast<BinaryOperator>(Op0))
1335  if (SI0->isShift() && SI0->getOpcode() == SI1->getOpcode() &&
1336  SI0->getOperand(1) == SI1->getOperand(1) &&
1337  (SI0->hasOneUse() || SI1->hasOneUse())) {
1338  Value *NewOp =
1339  Builder->CreateAnd(SI0->getOperand(0), SI1->getOperand(0),
1340  SI0->getName());
1341  return BinaryOperator::Create(SI1->getOpcode(), NewOp,
1342  SI1->getOperand(1));
1343  }
1344  }
1345 
1346  {
1347  Value *X = nullptr;
1348  bool OpsSwapped = false;
1349  // Canonicalize SExt or Not to the LHS
1350  if (match(Op1, m_SExt(m_Value())) ||
1351  match(Op1, m_Not(m_Value()))) {
1352  std::swap(Op0, Op1);
1353  OpsSwapped = true;
1354  }
1355 
1356  // Fold (and (sext bool to A), B) --> (select bool, B, 0)
1357  if (match(Op0, m_SExt(m_Value(X))) &&
1358  X->getType()->getScalarType()->isIntegerTy(1)) {
1359  Value *Zero = Constant::getNullValue(Op1->getType());
1360  return SelectInst::Create(X, Op1, Zero);
1361  }
1362 
1363  // Fold (and ~(sext bool to A), B) --> (select bool, 0, B)
1364  if (match(Op0, m_Not(m_SExt(m_Value(X)))) &&
1365  X->getType()->getScalarType()->isIntegerTy(1)) {
1366  Value *Zero = Constant::getNullValue(Op0->getType());
1367  return SelectInst::Create(X, Zero, Op1);
1368  }
1369 
1370  if (OpsSwapped)
1371  std::swap(Op0, Op1);
1372  }
1373 
1374  return Changed ? &I : nullptr;
1375 }
Instruction * FoldOpIntoPhi(Instruction &I)
BinaryOp_match< LHS, RHS, Instruction::And > m_And(const LHS &L, const RHS &R)
Definition: PatternMatch.h:467
Value * FoldAndOfICmps(ICmpInst *LHS, ICmpInst *RHS)
FoldAndOfICmps - Fold (icmp)&(icmp) if possible.
class_match< Value > m_Value()
m_Value() - Match an arbitrary value and ignore it.
Definition: PatternMatch.h:70
static BinaryOperator * CreateNot(Value *Op, const Twine &Name="", Instruction *InsertBefore=nullptr)
bool ShouldOptimizeCast(Instruction::CastOps opcode, const Value *V, Type *Ty)
match_zero m_Zero()
Definition: PatternMatch.h:137
This class represents zero extension of integer types.
Value * SimplifyVectorOp(BinaryOperator &Inst)
Makes transformation of binary operation specific for vector types.
static APInt getLowBitsSet(unsigned numBits, unsigned loBitsSet)
Get a value with low bits set.
Definition: APInt.h:526
static RegisterPass< NVPTXAllocaHoisting > X("alloca-hoisting","Hoisting alloca instructions in non-entry ""blocks to the entry block")
Instruction * FoldOpIntoSelect(Instruction &Op, SelectInst *SI)
Value * CreateICmpEQ(Value *LHS, Value *RHS, const Twine &Name="")
Definition: IRBuilder.h:1247
static Constant * getNullValue(Type *Ty)
Definition: Constants.cpp:133
StringRef getName() const
Definition: Value.cpp:168
bool match(Val *V, const Pattern &P)
Definition: PatternMatch.h:42
BinaryOp_match< LHS, RHS, Instruction::Xor > m_Xor(const LHS &L, const RHS &R)
Definition: PatternMatch.h:479
Base class of casting instructions.
Definition: InstrTypes.h:387
Instruction * OptAndOp(Instruction *Op, ConstantInt *OpRHS, ConstantInt *AndRHS, BinaryOperator &TheAnd)
CastClass_match< OpTy, Instruction::Trunc > m_Trunc(const OpTy &Op)
m_Trunc
Definition: PatternMatch.h:768
not_match< LHS > m_Not(const LHS &L)
Definition: PatternMatch.h:828
Value * CreateAnd(Value *LHS, Value *RHS, const Twine &Name="")
Definition: IRBuilder.h:806
static SelectInst * Create(Value *C, Value *S1, Value *S2, const Twine &NameStr="", Instruction *InsertBefore=nullptr)
Value * CreateOr(Value *LHS, Value *RHS, const Twine &Name="")
Definition: IRBuilder.h:822
Value * SimplifyUsingDistributiveLaws(BinaryOperator &I)
Represents a floating point comparison operator.
class_match< ConstantInt > m_ConstantInt()
m_ConstantInt() - Match an arbitrary ConstantInt and ignore it.
Definition: PatternMatch.h:72
bool isIntOrIntVectorTy() const
Definition: Type.h:201
BinaryOp_match< LHS, RHS, Instruction::Or > m_Or(const LHS &L, const RHS &R)
Definition: PatternMatch.h:473
Value * SimplifyAndInst(Value *LHS, Value *RHS, const DataLayout *TD=nullptr, const TargetLibraryInfo *TLI=nullptr, const DominatorTree *DT=nullptr)
Value * CreateNot(Value *V, const Twine &Name="")
Definition: IRBuilder.h:885
LLVM Constant Representation.
Definition: Constant.h:41
Value * FoldAndOfFCmps(FCmpInst *LHS, FCmpInst *RHS)
Instruction * ReplaceInstUsesWith(Instruction &I, Value *V)
Definition: InstCombine.h:282
static Constant * getAnd(Constant *C1, Constant *C2)
Definition: Constants.cpp:2161
APInt Or(const APInt &LHS, const APInt &RHS)
Bitwise OR function for APInt.
Definition: APInt.h:1870
APInt Xor(const APInt &LHS, const APInt &RHS)
Bitwise XOR function for APInt.
Definition: APInt.h:1875
specificval_ty m_Specific(const Value *V)
m_Specific - Match if we have a specific specified value.
Definition: PatternMatch.h:323
Represent an integer comparison operator.
Definition: Instructions.h:977
unsigned getBitWidth() const
Return the number of bits in the APInt.
Definition: APInt.h:1248
Value * getOperand(unsigned i) const
Definition: User.h:90
BuilderTy * Builder
Definition: InstCombine.h:102
CastClass_match< OpTy, Instruction::SExt > m_SExt(const OpTy &Op)
m_SExt
Definition: PatternMatch.h:775
Definition: test.h:1
bool SimplifyAssociativeOrCommutative(BinaryOperator &I)
Class for constant integers.
Definition: Constants.h:51
Type * getType() const
Definition: Value.h:215
static Constant * getTrunc(Constant *C, Type *Ty)
Definition: Constants.cpp:1587
void swap(llvm::BitVector &LHS, llvm::BitVector &RHS)
Implement std::swap in terms of BitVector swap.
Definition: BitVector.h:590
Class for arbitrary precision integers.
Definition: APInt.h:75
bool isIntegerTy() const
Definition: Type.h:193
static BinaryOperator * Create(BinaryOps Op, Value *S1, Value *S2, const Twine &Name=Twine(), Instruction *InsertBefore=nullptr)
const Type * getScalarType() const LLVM_READONLY
Definition: Type.cpp:51
static CastInst * Create(Instruction::CastOps, Value *S, Type *Ty, const Twine &Name="", Instruction *InsertBefore=nullptr)
Construct any of the CastInst subclasses.
static Value * dyn_castNotVal(Value *V)
#define I(x, y, z)
Definition: MD5.cpp:54
bool hasOneUse() const
Definition: Value.h:284
bool MaskedValueIsZero(Value *V, const APInt &Mask, unsigned Depth=0) const
Definition: InstCombine.h:323
const DataLayout * DL
Definition: InstCombine.h:89
Value * CreateNeg(Value *V, const Twine &Name="", bool HasNUW=false, bool HasNSW=false)
Definition: IRBuilder.h:863
Value * CreateTrunc(Value *V, Type *DestTy, const Twine &Name="")
Definition: IRBuilder.h:1094
LLVM Value Representation.
Definition: Value.h:69
unsigned countLeadingZeros() const
The APInt version of the countLeadingZeros functions in MathExtras.h.
Definition: APInt.h:1336
bool SimplifyDemandedInstructionBits(Instruction &Inst)
Value * FoldLogicalPlusAnd(Value *LHS, Value *RHS, ConstantInt *Mask, bool isSub, Instruction &I)
Instruction * InstCombiner::visitAShr ( BinaryOperator I)

Definition at line 772 of file InstCombineShifts.cpp.

772  {
773  if (Value *V = SimplifyVectorOp(I))
774  return ReplaceInstUsesWith(I, V);
775 
776  if (Value *V = SimplifyAShrInst(I.getOperand(0), I.getOperand(1),
777  I.isExact(), DL))
778  return ReplaceInstUsesWith(I, V);
779 
781  return R;
782 
783  Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
784 
785  if (ConstantInt *Op1C = dyn_cast<ConstantInt>(Op1)) {
786  unsigned ShAmt = Op1C->getZExtValue();
787 
788  // If the input is a SHL by the same constant (ashr (shl X, C), C), then we
789  // have a sign-extend idiom.
790  Value *X;
791  if (match(Op0, m_Shl(m_Value(X), m_Specific(Op1)))) {
792  // If the input is an extension from the shifted amount value, e.g.
793  // %x = zext i8 %A to i32
794  // %y = shl i32 %x, 24
795  // %z = ashr %y, 24
796  // then turn this into "z = sext i8 A to i32".
797  if (ZExtInst *ZI = dyn_cast<ZExtInst>(X)) {
798  uint32_t SrcBits = ZI->getOperand(0)->getType()->getScalarSizeInBits();
799  uint32_t DestBits = ZI->getType()->getScalarSizeInBits();
800  if (Op1C->getZExtValue() == DestBits-SrcBits)
801  return new SExtInst(ZI->getOperand(0), ZI->getType());
802  }
803  }
804 
805  // If the shifted-out value is known-zero, then this is an exact shift.
806  if (!I.isExact() &&
807  MaskedValueIsZero(Op0,APInt::getLowBitsSet(Op1C->getBitWidth(),ShAmt))){
808  I.setIsExact();
809  return &I;
810  }
811  }
812 
813  // See if we can turn a signed shr into an unsigned shr.
814  if (MaskedValueIsZero(Op0,
816  return BinaryOperator::CreateLShr(Op0, Op1);
817 
818  return nullptr;
819 }
class_match< Value > m_Value()
m_Value() - Match an arbitrary value and ignore it.
Definition: PatternMatch.h:70
static APInt getSignBit(unsigned BitWidth)
Get the SignBit for a specific bit width.
Definition: APInt.h:441
This class represents zero extension of integer types.
Value * SimplifyVectorOp(BinaryOperator &Inst)
Makes transformation of binary operation specific for vector types.
static APInt getLowBitsSet(unsigned numBits, unsigned loBitsSet)
Get a value with low bits set.
Definition: APInt.h:526
static RegisterPass< NVPTXAllocaHoisting > X("alloca-hoisting","Hoisting alloca instructions in non-entry ""blocks to the entry block")
This class represents a sign extension of integer types.
bool match(Val *V, const Pattern &P)
Definition: PatternMatch.h:42
Instruction * commonShiftTransforms(BinaryOperator &I)
Value * SimplifyAShrInst(Value *Op0, Value *Op1, bool isExact, const DataLayout *TD=nullptr, const TargetLibraryInfo *TLI=nullptr, const DominatorTree *DT=nullptr)
Instruction * ReplaceInstUsesWith(Instruction &I, Value *V)
Definition: InstCombine.h:282
specificval_ty m_Specific(const Value *V)
m_Specific - Match if we have a specific specified value.
Definition: PatternMatch.h:323
BinaryOp_match< LHS, RHS, Instruction::Shl > m_Shl(const LHS &L, const RHS &R)
Definition: PatternMatch.h:485
Value * getOperand(unsigned i) const
Definition: User.h:90
void setIsExact(bool b=true)
Class for constant integers.
Definition: Constants.h:51
unsigned getScalarSizeInBits() const LLVM_READONLY
Definition: Type.cpp:135
Type * getType() const
Definition: Value.h:215
bool isExact() const
isExact - Determine whether the exact flag is set.
#define I(x, y, z)
Definition: MD5.cpp:54
bool MaskedValueIsZero(Value *V, const APInt &Mask, unsigned Depth=0) const
Definition: InstCombine.h:323
const DataLayout * DL
Definition: InstCombine.h:89
LLVM Value Representation.
Definition: Value.h:69
Instruction * llvm::InstVisitor< InstCombiner , Instruction * >::visitAtomicCmpXchgInst ( AtomicCmpXchgInst I)
inlineinherited

Definition at line 177 of file InstVisitor.h.

#define DELEGATE(CLASS_TO_VISIT)
Definition: InstVisitor.h:30
Instruction * llvm::InstVisitor< InstCombiner , Instruction * >::visitAtomicRMWInst ( AtomicRMWInst I)
inlineinherited

Definition at line 178 of file InstVisitor.h.

#define DELEGATE(CLASS_TO_VISIT)
Definition: InstVisitor.h:30
void llvm::InstVisitor< InstCombiner , Instruction * >::visitBasicBlock ( BasicBlock BB)
inlineinherited

Definition at line 143 of file InstVisitor.h.

143 {}
Instruction * llvm::InstVisitor< InstCombiner , Instruction * >::visitBinaryOperator ( BinaryOperator I)
inlineinherited

Definition at line 232 of file InstVisitor.h.

#define DELEGATE(CLASS_TO_VISIT)
Definition: InstVisitor.h:30
Instruction * InstCombiner::visitBitCast ( BitCastInst CI)

Definition at line 1783 of file InstCombineCasts.cpp.

1783  {
1784  // If the operands are integer typed then apply the integer transforms,
1785  // otherwise just apply the common ones.
1786  Value *Src = CI.getOperand(0);
1787  Type *SrcTy = Src->getType();
1788  Type *DestTy = CI.getType();
1789 
1790  // Get rid of casts from one type to the same type. These are useless and can
1791  // be replaced by the operand.
1792  if (DestTy == Src->getType())
1793  return ReplaceInstUsesWith(CI, Src);
1794 
1795  if (PointerType *DstPTy = dyn_cast<PointerType>(DestTy)) {
1796  PointerType *SrcPTy = cast<PointerType>(SrcTy);
1797  Type *DstElTy = DstPTy->getElementType();
1798  Type *SrcElTy = SrcPTy->getElementType();
1799 
1800  // If we are casting a alloca to a pointer to a type of the same
1801  // size, rewrite the allocation instruction to allocate the "right" type.
1802  // There is no need to modify malloc calls because it is their bitcast that
1803  // needs to be cleaned up.
1804  if (AllocaInst *AI = dyn_cast<AllocaInst>(Src))
1805  if (Instruction *V = PromoteCastOfAllocation(CI, *AI))
1806  return V;
1807 
1808  // If the source and destination are pointers, and this cast is equivalent
1809  // to a getelementptr X, 0, 0, 0... turn it into the appropriate gep.
1810  // This can enhance SROA and other transforms that want type-safe pointers.
1811  Constant *ZeroUInt =
1813  unsigned NumZeros = 0;
1814  while (SrcElTy != DstElTy &&
1815  isa<CompositeType>(SrcElTy) && !SrcElTy->isPointerTy() &&
1816  SrcElTy->getNumContainedTypes() /* not "{}" */) {
1817  SrcElTy = cast<CompositeType>(SrcElTy)->getTypeAtIndex(ZeroUInt);
1818  ++NumZeros;
1819  }
1820 
1821  // If we found a path from the src to dest, create the getelementptr now.
1822  if (SrcElTy == DstElTy) {
1823  SmallVector<Value*, 8> Idxs(NumZeros+1, ZeroUInt);
1824  return GetElementPtrInst::CreateInBounds(Src, Idxs);
1825  }
1826  }
1827 
1828  // Try to optimize int -> float bitcasts.
1829  if ((DestTy->isFloatTy() || DestTy->isDoubleTy()) && isa<IntegerType>(SrcTy))
1830  if (Instruction *I = OptimizeIntToFloatBitCast(CI, *this))
1831  return I;
1832 
1833  if (VectorType *DestVTy = dyn_cast<VectorType>(DestTy)) {
1834  if (DestVTy->getNumElements() == 1 && !SrcTy->isVectorTy()) {
1835  Value *Elem = Builder->CreateBitCast(Src, DestVTy->getElementType());
1836  return InsertElementInst::Create(UndefValue::get(DestTy), Elem,
1838  // FIXME: Canonicalize bitcast(insertelement) -> insertelement(bitcast)
1839  }
1840 
1841  if (isa<IntegerType>(SrcTy)) {
1842  // If this is a cast from an integer to vector, check to see if the input
1843  // is a trunc or zext of a bitcast from vector. If so, we can replace all
1844  // the casts with a shuffle and (potentially) a bitcast.
1845  if (isa<TruncInst>(Src) || isa<ZExtInst>(Src)) {
1846  CastInst *SrcCast = cast<CastInst>(Src);
1847  if (BitCastInst *BCIn = dyn_cast<BitCastInst>(SrcCast->getOperand(0)))
1848  if (isa<VectorType>(BCIn->getOperand(0)->getType()))
1849  if (Instruction *I = OptimizeVectorResize(BCIn->getOperand(0),
1850  cast<VectorType>(DestTy), *this))
1851  return I;
1852  }
1853 
1854  // If the input is an 'or' instruction, we may be doing shifts and ors to
1855  // assemble the elements of the vector manually. Try to rip the code out
1856  // and replace it with insertelements.
1857  if (Value *V = OptimizeIntegerToVectorInsertions(CI, *this))
1858  return ReplaceInstUsesWith(CI, V);
1859  }
1860  }
1861 
1862  if (VectorType *SrcVTy = dyn_cast<VectorType>(SrcTy)) {
1863  if (SrcVTy->getNumElements() == 1) {
1864  // If our destination is not a vector, then make this a straight
1865  // scalar-scalar cast.
1866  if (!DestTy->isVectorTy()) {
1867  Value *Elem =
1870  return CastInst::Create(Instruction::BitCast, Elem, DestTy);
1871  }
1872 
1873  // Otherwise, see if our source is an insert. If so, then use the scalar
1874  // component directly.
1875  if (InsertElementInst *IEI =
1876  dyn_cast<InsertElementInst>(CI.getOperand(0)))
1877  return CastInst::Create(Instruction::BitCast, IEI->getOperand(1),
1878  DestTy);
1879  }
1880  }
1881 
1882  if (ShuffleVectorInst *SVI = dyn_cast<ShuffleVectorInst>(Src)) {
1883  // Okay, we have (bitcast (shuffle ..)). Check to see if this is
1884  // a bitcast to a vector with the same # elts.
1885  if (SVI->hasOneUse() && DestTy->isVectorTy() &&
1886  DestTy->getVectorNumElements() == SVI->getType()->getNumElements() &&
1887  SVI->getType()->getNumElements() ==
1888  SVI->getOperand(0)->getType()->getVectorNumElements()) {
1889  BitCastInst *Tmp;
1890  // If either of the operands is a cast from CI.getType(), then
1891  // evaluating the shuffle in the casted destination's type will allow
1892  // us to eliminate at least one cast.
1893  if (((Tmp = dyn_cast<BitCastInst>(SVI->getOperand(0))) &&
1894  Tmp->getOperand(0)->getType() == DestTy) ||
1895  ((Tmp = dyn_cast<BitCastInst>(SVI->getOperand(1))) &&
1896  Tmp->getOperand(0)->getType() == DestTy)) {
1897  Value *LHS = Builder->CreateBitCast(SVI->getOperand(0), DestTy);
1898  Value *RHS = Builder->CreateBitCast(SVI->getOperand(1), DestTy);
1899  // Return a new shuffle vector. Use the same element ID's, as we
1900  // know the vector types match #elts.
1901  return new ShuffleVectorInst(LHS, RHS, SVI->getOperand(2));
1902  }
1903  }
1904  }
1905 
1906  if (SrcTy->isPointerTy())
1907  return commonPointerCastTransforms(CI);
1908  return commonCastTransforms(CI);
1909 }
Instruction * commonCastTransforms(CastInst &CI)
Implement the transforms common to all CastInst visitors.
bool isDoubleTy() const
isDoubleTy - Return true if this is 'double', a 64-bit IEEE fp type.
Definition: Type.h:146
static Constant * getNullValue(Type *Ty)
Definition: Constants.cpp:133
static InsertElementInst * Create(Value *Vec, Value *NewElt, Value *Idx, const Twine &NameStr="", Instruction *InsertBefore=nullptr)
Base class of casting instructions.
Definition: InstrTypes.h:387
static GetElementPtrInst * CreateInBounds(Value *Ptr, ArrayRef< Value * > IdxList, const Twine &NameStr="", Instruction *InsertBefore=nullptr)
Definition: Instructions.h:809
This class represents a no-op cast from one type to another.
Type * getElementType() const
Definition: DerivedTypes.h:319
bool isVectorTy() const
Definition: Type.h:226
LLVM Constant Representation.
Definition: Constant.h:41
Instruction * ReplaceInstUsesWith(Instruction &I, Value *V)
Definition: InstCombine.h:282
bool isFloatTy() const
isFloatTy - Return true if this is 'float', a 32-bit IEEE fp type.
Definition: Type.h:143
Instruction * PromoteCastOfAllocation(BitCastInst &CI, AllocaInst &AI)
Value * getOperand(unsigned i) const
Definition: User.h:90
static Instruction * OptimizeVectorResize(Value *InVal, VectorType *DestTy, InstCombiner &IC)
unsigned getNumContainedTypes() const
Definition: Type.h:336
BuilderTy * Builder
Definition: InstCombine.h:102
bool isPointerTy() const
Definition: Type.h:217
static UndefValue * get(Type *T)
Definition: Constants.cpp:1372
LLVMContext & getContext() const
All values hold a context through their type.
Definition: Value.cpp:615
static Value * OptimizeIntegerToVectorInsertions(BitCastInst &CI, InstCombiner &IC)
Value * CreateExtractElement(Value *Vec, Value *Idx, const Twine &Name="")
Definition: IRBuilder.h:1390
unsigned getVectorNumElements() const
Definition: Type.cpp:208
Value * CreateBitCast(Value *V, Type *DestTy, const Twine &Name="")
Definition: IRBuilder.h:1158
Type * getType() const
Definition: Value.h:215
static Instruction * OptimizeIntToFloatBitCast(BitCastInst &CI, InstCombiner &IC)
static CastInst * Create(Instruction::CastOps, Value *S, Type *Ty, const Twine &Name="", Instruction *InsertBefore=nullptr)
Construct any of the CastInst subclasses.
static IntegerType * getInt32Ty(LLVMContext &C)
Definition: Type.cpp:235
#define I(x, y, z)
Definition: MD5.cpp:54
LLVM Value Representation.
Definition: Value.h:69
Instruction * commonPointerCastTransforms(CastInst &CI)
Implement the transforms for cast of pointer (bitcast/ptrtoint)
Instruction * llvm::InstVisitor< InstCombiner , Instruction * >::visitBitCastInst ( BitCastInst I)
inlineinherited

Definition at line 193 of file InstVisitor.h.

193 { DELEGATE(CastInst);}
#define DELEGATE(CLASS_TO_VISIT)
Definition: InstVisitor.h:30
Instruction * InstCombiner::visitBranchInst ( BranchInst BI)

Definition at line 1927 of file InstructionCombining.cpp.

1927  {
1928  // Change br (not X), label True, label False to: br X, label False, True
1929  Value *X = nullptr;
1930  BasicBlock *TrueDest;
1931  BasicBlock *FalseDest;
1932  if (match(&BI, m_Br(m_Not(m_Value(X)), TrueDest, FalseDest)) &&
1933  !isa<Constant>(X)) {
1934  // Swap Destinations and condition...
1935  BI.setCondition(X);
1936  BI.swapSuccessors();
1937  return &BI;
1938  }
1939 
1940  // Canonicalize fcmp_one -> fcmp_oeq
1941  FCmpInst::Predicate FPred; Value *Y;
1942  if (match(&BI, m_Br(m_FCmp(FPred, m_Value(X), m_Value(Y)),
1943  TrueDest, FalseDest)) &&
1944  BI.getCondition()->hasOneUse())
1945  if (FPred == FCmpInst::FCMP_ONE || FPred == FCmpInst::FCMP_OLE ||
1946  FPred == FCmpInst::FCMP_OGE) {
1947  FCmpInst *Cond = cast<FCmpInst>(BI.getCondition());
1949 
1950  // Swap Destinations and condition.
1951  BI.swapSuccessors();
1952  Worklist.Add(Cond);
1953  return &BI;
1954  }
1955 
1956  // Canonicalize icmp_ne -> icmp_eq
1957  ICmpInst::Predicate IPred;
1958  if (match(&BI, m_Br(m_ICmp(IPred, m_Value(X), m_Value(Y)),
1959  TrueDest, FalseDest)) &&
1960  BI.getCondition()->hasOneUse())
1961  if (IPred == ICmpInst::ICMP_NE || IPred == ICmpInst::ICMP_ULE ||
1962  IPred == ICmpInst::ICMP_SLE || IPred == ICmpInst::ICMP_UGE ||
1963  IPred == ICmpInst::ICMP_SGE) {
1964  ICmpInst *Cond = cast<ICmpInst>(BI.getCondition());
1966  // Swap Destinations and condition.
1967  BI.swapSuccessors();
1968  Worklist.Add(Cond);
1969  return &BI;
1970  }
1971 
1972  return nullptr;
1973 }
class_match< Value > m_Value()
m_Value() - Match an arbitrary value and ignore it.
Definition: PatternMatch.h:70
void swapSuccessors()
Swap the successors of this branch instruction.
Predicate getInversePredicate() const
Return the inverse of the instruction's predicate.
Definition: InstrTypes.h:758
unsigned less or equal
Definition: InstrTypes.h:698
static RegisterPass< NVPTXAllocaHoisting > X("alloca-hoisting","Hoisting alloca instructions in non-entry ""blocks to the entry block")
void Add(Instruction *I)
bool match(Val *V, const Pattern &P)
Definition: PatternMatch.h:42
CmpClass_match< LHS, RHS, FCmpInst, FCmpInst::Predicate > m_FCmp(FCmpInst::Predicate &Pred, const LHS &L, const RHS &R)
Definition: PatternMatch.h:689
0 1 0 1 True if ordered and less than or equal
Definition: InstrTypes.h:679
not_match< LHS > m_Not(const LHS &L)
Definition: PatternMatch.h:828
Represents a floating point comparison operator.
LLVM Basic Block Representation.
Definition: BasicBlock.h:72
brc_match< Cond_t > m_Br(const Cond_t &C, BasicBlock *&T, BasicBlock *&F)
Definition: PatternMatch.h:927
Represent an integer comparison operator.
Definition: Instructions.h:977
InstCombineWorklist Worklist
Worklist - All of the instructions that need to be simplified.
Definition: InstCombine.h:97
void setPredicate(Predicate P)
Set the predicate for this instruction to the specified value.
Definition: InstrTypes.h:740
signed less or equal
Definition: InstrTypes.h:702
Value * getCondition() const
unsigned greater or equal
Definition: InstrTypes.h:696
bool hasOneUse() const
Definition: Value.h:284
0 1 1 0 True if ordered and operands are unequal
Definition: InstrTypes.h:680
void setCondition(Value *V)
LLVM Value Representation.
Definition: Value.h:69
static GCMetadataPrinterRegistry::Add< OcamlGCMetadataPrinter > Y("ocaml","ocaml 3.10-compatible collector")
0 0 1 1 True if ordered and greater than or equal
Definition: InstrTypes.h:677
signed greater or equal
Definition: InstrTypes.h:700
CmpClass_match< LHS, RHS, ICmpInst, ICmpInst::Predicate > m_ICmp(ICmpInst::Predicate &Pred, const LHS &L, const RHS &R)
Definition: PatternMatch.h:682
Instruction * InstCombiner::visitCallInst ( CallInst CI)

visitCallInst - CallInst simplification. This mostly only handles folding of intrinsic instructions. For normal calls, it allows visitCallSite to do the heavy lifting.

Definition at line 203 of file InstCombineCalls.cpp.

203  {
204  if (isFreeCall(&CI, TLI))
205  return visitFree(CI);
206 
207  // If the caller function is nounwind, mark the call as nounwind, even if the
208  // callee isn't.
209  if (CI.getParent()->getParent()->doesNotThrow() &&
210  !CI.doesNotThrow()) {
211  CI.setDoesNotThrow();
212  return &CI;
213  }
214 
216  if (!II) return visitCallSite(&CI);
217 
218  // Intrinsics cannot occur in an invoke, so handle them here instead of in
219  // visitCallSite.
220  if (MemIntrinsic *MI = dyn_cast<MemIntrinsic>(II)) {
221  bool Changed = false;
222 
223  // memmove/cpy/set of zero bytes is a noop.
224  if (Constant *NumBytes = dyn_cast<Constant>(MI->getLength())) {
225  if (NumBytes->isNullValue())
226  return EraseInstFromFunction(CI);
227 
228  if (ConstantInt *CI = dyn_cast<ConstantInt>(NumBytes))
229  if (CI->getZExtValue() == 1) {
230  // Replace the instruction with just byte operations. We would
231  // transform other cases to loads/stores, but we don't know if
232  // alignment is sufficient.
233  }
234  }
235 
236  // No other transformations apply to volatile transfers.
237  if (MI->isVolatile())
238  return nullptr;
239 
240  // If we have a memmove and the source operation is a constant global,
241  // then the source and dest pointers can't alias, so we can change this
242  // into a call to memcpy.
243  if (MemMoveInst *MMI = dyn_cast<MemMoveInst>(MI)) {
244  if (GlobalVariable *GVSrc = dyn_cast<GlobalVariable>(MMI->getSource()))
245  if (GVSrc->isConstant()) {
246  Module *M = CI.getParent()->getParent()->getParent();
247  Intrinsic::ID MemCpyID = Intrinsic::memcpy;
248  Type *Tys[3] = { CI.getArgOperand(0)->getType(),
249  CI.getArgOperand(1)->getType(),
250  CI.getArgOperand(2)->getType() };
251  CI.setCalledFunction(Intrinsic::getDeclaration(M, MemCpyID, Tys));
252  Changed = true;
253  }
254  }
255 
256  if (MemTransferInst *MTI = dyn_cast<MemTransferInst>(MI)) {
257  // memmove(x,x,size) -> noop.
258  if (MTI->getSource() == MTI->getDest())
259  return EraseInstFromFunction(CI);
260  }
261 
262  // If we can determine a pointer alignment that is bigger than currently
263  // set, update the alignment.
264  if (isa<MemTransferInst>(MI)) {
266  return I;
267  } else if (MemSetInst *MSI = dyn_cast<MemSetInst>(MI)) {
268  if (Instruction *I = SimplifyMemSet(MSI))
269  return I;
270  }
271 
272  if (Changed) return II;
273  }
274 
275  switch (II->getIntrinsicID()) {
276  default: break;
277  case Intrinsic::objectsize: {
278  uint64_t Size;
279  if (getObjectSize(II->getArgOperand(0), Size, DL, TLI))
280  return ReplaceInstUsesWith(CI, ConstantInt::get(CI.getType(), Size));
281  return nullptr;
282  }
283  case Intrinsic::bswap: {
284  Value *IIOperand = II->getArgOperand(0);
285  Value *X = nullptr;
286 
287  // bswap(bswap(x)) -> x
288  if (match(IIOperand, m_BSwap(m_Value(X))))
289  return ReplaceInstUsesWith(CI, X);
290 
291  // bswap(trunc(bswap(x))) -> trunc(lshr(x, c))
292  if (match(IIOperand, m_Trunc(m_BSwap(m_Value(X))))) {
293  unsigned C = X->getType()->getPrimitiveSizeInBits() -
294  IIOperand->getType()->getPrimitiveSizeInBits();
295  Value *CV = ConstantInt::get(X->getType(), C);
296  Value *V = Builder->CreateLShr(X, CV);
297  return new TruncInst(V, IIOperand->getType());
298  }
299  break;
300  }
301 
302  case Intrinsic::powi:
303  if (ConstantInt *Power = dyn_cast<ConstantInt>(II->getArgOperand(1))) {
304  // powi(x, 0) -> 1.0
305  if (Power->isZero())
306  return ReplaceInstUsesWith(CI, ConstantFP::get(CI.getType(), 1.0));
307  // powi(x, 1) -> x
308  if (Power->isOne())
309  return ReplaceInstUsesWith(CI, II->getArgOperand(0));
310  // powi(x, -1) -> 1/x
311  if (Power->isAllOnesValue())
312  return BinaryOperator::CreateFDiv(ConstantFP::get(CI.getType(), 1.0),
313  II->getArgOperand(0));
314  }
315  break;
316  case Intrinsic::cttz: {
317  // If all bits below the first known one are known zero,
318  // this value is constant.
320  // FIXME: Try to simplify vectors of integers.
321  if (!IT) break;
322  uint32_t BitWidth = IT->getBitWidth();
323  APInt KnownZero(BitWidth, 0);
324  APInt KnownOne(BitWidth, 0);
325  computeKnownBits(II->getArgOperand(0), KnownZero, KnownOne);
326  unsigned TrailingZeros = KnownOne.countTrailingZeros();
327  APInt Mask(APInt::getLowBitsSet(BitWidth, TrailingZeros));
328  if ((Mask & KnownZero) == Mask)
330  APInt(BitWidth, TrailingZeros)));
331 
332  }
333  break;
334  case Intrinsic::ctlz: {
335  // If all bits above the first known one are known zero,
336  // this value is constant.
338  // FIXME: Try to simplify vectors of integers.
339  if (!IT) break;
340  uint32_t BitWidth = IT->getBitWidth();
341  APInt KnownZero(BitWidth, 0);
342  APInt KnownOne(BitWidth, 0);
343  computeKnownBits(II->getArgOperand(0), KnownZero, KnownOne);
344  unsigned LeadingZeros = KnownOne.countLeadingZeros();
345  APInt Mask(APInt::getHighBitsSet(BitWidth, LeadingZeros));
346  if ((Mask & KnownZero) == Mask)
348  APInt(BitWidth, LeadingZeros)));
349 
350  }
351  break;
352  case Intrinsic::uadd_with_overflow: {
353  Value *LHS = II->getArgOperand(0), *RHS = II->getArgOperand(1);
354  IntegerType *IT = cast<IntegerType>(II->getArgOperand(0)->getType());
355  uint32_t BitWidth = IT->getBitWidth();
356  APInt LHSKnownZero(BitWidth, 0);
357  APInt LHSKnownOne(BitWidth, 0);
358  computeKnownBits(LHS, LHSKnownZero, LHSKnownOne);
359  bool LHSKnownNegative = LHSKnownOne[BitWidth - 1];
360  bool LHSKnownPositive = LHSKnownZero[BitWidth - 1];
361 
362  if (LHSKnownNegative || LHSKnownPositive) {
363  APInt RHSKnownZero(BitWidth, 0);
364  APInt RHSKnownOne(BitWidth, 0);
365  computeKnownBits(RHS, RHSKnownZero, RHSKnownOne);
366  bool RHSKnownNegative = RHSKnownOne[BitWidth - 1];
367  bool RHSKnownPositive = RHSKnownZero[BitWidth - 1];
368  if (LHSKnownNegative && RHSKnownNegative) {
369  // The sign bit is set in both cases: this MUST overflow.
370  // Create a simple add instruction, and insert it into the struct.
371  Value *Add = Builder->CreateAdd(LHS, RHS);
372  Add->takeName(&CI);
373  Constant *V[] = {
374  UndefValue::get(LHS->getType()),
376  };
377  StructType *ST = cast<StructType>(II->getType());
378  Constant *Struct = ConstantStruct::get(ST, V);
379  return InsertValueInst::Create(Struct, Add, 0);
380  }
381 
382  if (LHSKnownPositive && RHSKnownPositive) {
383  // The sign bit is clear in both cases: this CANNOT overflow.
384  // Create a simple add instruction, and insert it into the struct.
385  Value *Add = Builder->CreateNUWAdd(LHS, RHS);
386  Add->takeName(&CI);
387  Constant *V[] = {
388  UndefValue::get(LHS->getType()),
390  };
391  StructType *ST = cast<StructType>(II->getType());
392  Constant *Struct = ConstantStruct::get(ST, V);
393  return InsertValueInst::Create(Struct, Add, 0);
394  }
395  }
396  }
397  // FALL THROUGH uadd into sadd
398  case Intrinsic::sadd_with_overflow:
399  // Canonicalize constants into the RHS.
400  if (isa<Constant>(II->getArgOperand(0)) &&
401  !isa<Constant>(II->getArgOperand(1))) {
402  Value *LHS = II->getArgOperand(0);
403  II->setArgOperand(0, II->getArgOperand(1));
404  II->setArgOperand(1, LHS);
405  return II;
406  }
407 
408  // X + undef -> undef
409  if (isa<UndefValue>(II->getArgOperand(1)))
410  return ReplaceInstUsesWith(CI, UndefValue::get(II->getType()));
411 
412  if (ConstantInt *RHS = dyn_cast<ConstantInt>(II->getArgOperand(1))) {
413  // X + 0 -> {X, false}
414  if (RHS->isZero()) {
415  Constant *V[] = {
418  };
419  Constant *Struct =
420  ConstantStruct::get(cast<StructType>(II->getType()), V);
421  return InsertValueInst::Create(Struct, II->getArgOperand(0), 0);
422  }
423  }
424 
425  // We can strength reduce reduce this signed add into a regular add if we
426  // can prove that it will never overflow.
427  if (II->getIntrinsicID() == Intrinsic::sadd_with_overflow) {
428  Value *LHS = II->getArgOperand(0), *RHS = II->getArgOperand(1);
429  if (WillNotOverflowSignedAdd(LHS, RHS)) {
430  Value *Add = Builder->CreateNSWAdd(LHS, RHS);
431  Add->takeName(&CI);
432  Constant *V[] = {UndefValue::get(Add->getType()), Builder->getFalse()};
433  StructType *ST = cast<StructType>(II->getType());
434  Constant *Struct = ConstantStruct::get(ST, V);
435  return InsertValueInst::Create(Struct, Add, 0);
436  }
437  }
438 
439  break;
440  case Intrinsic::usub_with_overflow:
441  case Intrinsic::ssub_with_overflow:
442  // undef - X -> undef
443  // X - undef -> undef
444  if (isa<UndefValue>(II->getArgOperand(0)) ||
445  isa<UndefValue>(II->getArgOperand(1)))
446  return ReplaceInstUsesWith(CI, UndefValue::get(II->getType()));
447 
448  if (ConstantInt *RHS = dyn_cast<ConstantInt>(II->getArgOperand(1))) {
449  // X - 0 -> {X, false}
450  if (RHS->isZero()) {
451  Constant *V[] = {
454  };
455  Constant *Struct =
456  ConstantStruct::get(cast<StructType>(II->getType()), V);
457  return InsertValueInst::Create(Struct, II->getArgOperand(0), 0);
458  }
459  }
460  break;
461  case Intrinsic::umul_with_overflow: {
462  Value *LHS = II->getArgOperand(0), *RHS = II->getArgOperand(1);
463  unsigned BitWidth = cast<IntegerType>(LHS->getType())->getBitWidth();
464 
465  APInt LHSKnownZero(BitWidth, 0);
466  APInt LHSKnownOne(BitWidth, 0);
467  computeKnownBits(LHS, LHSKnownZero, LHSKnownOne);
468  APInt RHSKnownZero(BitWidth, 0);
469  APInt RHSKnownOne(BitWidth, 0);
470  computeKnownBits(RHS, RHSKnownZero, RHSKnownOne);
471 
472  // Get the largest possible values for each operand.
473  APInt LHSMax = ~LHSKnownZero;
474  APInt RHSMax = ~RHSKnownZero;
475 
476  // If multiplying the maximum values does not overflow then we can turn
477  // this into a plain NUW mul.
478  bool Overflow;
479  LHSMax.umul_ov(RHSMax, Overflow);
480  if (!Overflow) {
481  Value *Mul = Builder->CreateNUWMul(LHS, RHS, "umul_with_overflow");
482  Constant *V[] = {
483  UndefValue::get(LHS->getType()),
484  Builder->getFalse()
485  };
486  Constant *Struct = ConstantStruct::get(cast<StructType>(II->getType()),V);
487  return InsertValueInst::Create(Struct, Mul, 0);
488  }
489  } // FALL THROUGH
490  case Intrinsic::smul_with_overflow:
491  // Canonicalize constants into the RHS.
492  if (isa<Constant>(II->getArgOperand(0)) &&
493  !isa<Constant>(II->getArgOperand(1))) {
494  Value *LHS = II->getArgOperand(0);
495  II->setArgOperand(0, II->getArgOperand(1));
496  II->setArgOperand(1, LHS);
497  return II;
498  }
499 
500  // X * undef -> undef
501  if (isa<UndefValue>(II->getArgOperand(1)))
502  return ReplaceInstUsesWith(CI, UndefValue::get(II->getType()));
503 
504  if (ConstantInt *RHSI = dyn_cast<ConstantInt>(II->getArgOperand(1))) {
505  // X*0 -> {0, false}
506  if (RHSI->isZero())
508 
509  // X * 1 -> {X, false}
510  if (RHSI->equalsInt(1)) {
511  Constant *V[] = {
514  };
515  Constant *Struct =
516  ConstantStruct::get(cast<StructType>(II->getType()), V);
517  return InsertValueInst::Create(Struct, II->getArgOperand(0), 0);
518  }
519  }
520  break;
521  case Intrinsic::ppc_altivec_lvx:
522  case Intrinsic::ppc_altivec_lvxl:
523  // Turn PPC lvx -> load if the pointer is known aligned.
524  if (getOrEnforceKnownAlignment(II->getArgOperand(0), 16, DL) >= 16) {
525  Value *Ptr = Builder->CreateBitCast(II->getArgOperand(0),
527  return new LoadInst(Ptr);
528  }
529  break;
530  case Intrinsic::ppc_altivec_stvx:
531  case Intrinsic::ppc_altivec_stvxl:
532  // Turn stvx -> store if the pointer is known aligned.
533  if (getOrEnforceKnownAlignment(II->getArgOperand(1), 16, DL) >= 16) {
534  Type *OpPtrTy =
536  Value *Ptr = Builder->CreateBitCast(II->getArgOperand(1), OpPtrTy);
537  return new StoreInst(II->getArgOperand(0), Ptr);
538  }
539  break;
540  case Intrinsic::x86_sse_storeu_ps:
541  case Intrinsic::x86_sse2_storeu_pd:
542  case Intrinsic::x86_sse2_storeu_dq:
543  // Turn X86 storeu -> store if the pointer is known aligned.
544  if (getOrEnforceKnownAlignment(II->getArgOperand(0), 16, DL) >= 16) {
545  Type *OpPtrTy =
547  Value *Ptr = Builder->CreateBitCast(II->getArgOperand(0), OpPtrTy);
548  return new StoreInst(II->getArgOperand(1), Ptr);
549  }
550  break;
551 
552  case Intrinsic::x86_sse_cvtss2si:
553  case Intrinsic::x86_sse_cvtss2si64:
554  case Intrinsic::x86_sse_cvttss2si:
555  case Intrinsic::x86_sse_cvttss2si64:
556  case Intrinsic::x86_sse2_cvtsd2si:
557  case Intrinsic::x86_sse2_cvtsd2si64:
558  case Intrinsic::x86_sse2_cvttsd2si:
559  case Intrinsic::x86_sse2_cvttsd2si64: {
560  // These intrinsics only demand the 0th element of their input vectors. If
561  // we can simplify the input based on that, do so now.
562  unsigned VWidth =
563  cast<VectorType>(II->getArgOperand(0)->getType())->getNumElements();
564  APInt DemandedElts(VWidth, 1);
565  APInt UndefElts(VWidth, 0);
567  DemandedElts, UndefElts)) {
568  II->setArgOperand(0, V);
569  return II;
570  }
571  break;
572  }
573 
574  // Constant fold <A x Bi> << Ci.
575  // FIXME: We don't handle _dq because it's a shift of an i128, but is
576  // represented in the IR as <2 x i64>. A per element shift is wrong.
577  case Intrinsic::x86_sse2_psll_d:
578  case Intrinsic::x86_sse2_psll_q:
579  case Intrinsic::x86_sse2_psll_w:
580  case Intrinsic::x86_sse2_pslli_d:
581  case Intrinsic::x86_sse2_pslli_q:
582  case Intrinsic::x86_sse2_pslli_w:
583  case Intrinsic::x86_avx2_psll_d:
584  case Intrinsic::x86_avx2_psll_q:
585  case Intrinsic::x86_avx2_psll_w:
586  case Intrinsic::x86_avx2_pslli_d:
587  case Intrinsic::x86_avx2_pslli_q:
588  case Intrinsic::x86_avx2_pslli_w:
589  case Intrinsic::x86_sse2_psrl_d:
590  case Intrinsic::x86_sse2_psrl_q:
591  case Intrinsic::x86_sse2_psrl_w:
592  case Intrinsic::x86_sse2_psrli_d:
593  case Intrinsic::x86_sse2_psrli_q:
594  case Intrinsic::x86_sse2_psrli_w:
595  case Intrinsic::x86_avx2_psrl_d:
596  case Intrinsic::x86_avx2_psrl_q:
597  case Intrinsic::x86_avx2_psrl_w:
598  case Intrinsic::x86_avx2_psrli_d:
599  case Intrinsic::x86_avx2_psrli_q:
600  case Intrinsic::x86_avx2_psrli_w: {
601  // Simplify if count is constant. To 0 if >= BitWidth,
602  // otherwise to shl/lshr.
603  auto CDV = dyn_cast<ConstantDataVector>(II->getArgOperand(1));
604  auto CInt = dyn_cast<ConstantInt>(II->getArgOperand(1));
605  if (!CDV && !CInt)
606  break;
607  ConstantInt *Count;
608  if (CDV)
609  Count = cast<ConstantInt>(CDV->getElementAsConstant(0));
610  else
611  Count = CInt;
612 
613  auto Vec = II->getArgOperand(0);
614  auto VT = cast<VectorType>(Vec->getType());
615  if (Count->getZExtValue() >
616  VT->getElementType()->getPrimitiveSizeInBits() - 1)
617  return ReplaceInstUsesWith(
618  CI, ConstantAggregateZero::get(Vec->getType()));
619 
620  bool isPackedShiftLeft = true;
621  switch (II->getIntrinsicID()) {
622  default : break;
623  case Intrinsic::x86_sse2_psrl_d:
624  case Intrinsic::x86_sse2_psrl_q:
625  case Intrinsic::x86_sse2_psrl_w:
626  case Intrinsic::x86_sse2_psrli_d:
627  case Intrinsic::x86_sse2_psrli_q:
628  case Intrinsic::x86_sse2_psrli_w:
629  case Intrinsic::x86_avx2_psrl_d:
630  case Intrinsic::x86_avx2_psrl_q:
631  case Intrinsic::x86_avx2_psrl_w:
632  case Intrinsic::x86_avx2_psrli_d:
633  case Intrinsic::x86_avx2_psrli_q:
634  case Intrinsic::x86_avx2_psrli_w: isPackedShiftLeft = false; break;
635  }
636 
637  unsigned VWidth = VT->getNumElements();
638  // Get a constant vector of the same type as the first operand.
639  auto VTCI = ConstantInt::get(VT->getElementType(), Count->getZExtValue());
640  if (isPackedShiftLeft)
641  return BinaryOperator::CreateShl(Vec,
642  Builder->CreateVectorSplat(VWidth, VTCI));
643 
644  return BinaryOperator::CreateLShr(Vec,
645  Builder->CreateVectorSplat(VWidth, VTCI));
646  }
647 
648  case Intrinsic::x86_sse41_pmovsxbw:
649  case Intrinsic::x86_sse41_pmovsxwd:
650  case Intrinsic::x86_sse41_pmovsxdq:
651  case Intrinsic::x86_sse41_pmovzxbw:
652  case Intrinsic::x86_sse41_pmovzxwd:
653  case Intrinsic::x86_sse41_pmovzxdq: {
654  // pmov{s|z}x ignores the upper half of their input vectors.
655  unsigned VWidth =
656  cast<VectorType>(II->getArgOperand(0)->getType())->getNumElements();
657  unsigned LowHalfElts = VWidth / 2;
658  APInt InputDemandedElts(APInt::getBitsSet(VWidth, 0, LowHalfElts));
659  APInt UndefElts(VWidth, 0);
661  InputDemandedElts,
662  UndefElts)) {
663  II->setArgOperand(0, TmpV);
664  return II;
665  }
666  break;
667  }
668 
669  case Intrinsic::x86_sse4a_insertqi: {
670  // insertqi x, y, 64, 0 can just copy y's lower bits and leave the top
671  // ones undef
672  // TODO: eventually we should lower this intrinsic to IR
673  if (auto CIWidth = dyn_cast<ConstantInt>(II->getArgOperand(2))) {
674  if (auto CIStart = dyn_cast<ConstantInt>(II->getArgOperand(3))) {
675  if (CIWidth->equalsInt(64) && CIStart->isZero()) {
676  Value *Vec = II->getArgOperand(1);
677  Value *Undef = UndefValue::get(Vec->getType());
678  const uint32_t Mask[] = { 0, 2 };
679  return ReplaceInstUsesWith(
680  CI,
682  Vec, Undef, ConstantDataVector::get(
683  II->getContext(), ArrayRef<uint32_t>(Mask))));
684 
685  } else if (auto Source =
686  dyn_cast<IntrinsicInst>(II->getArgOperand(0))) {
687  if (Source->hasOneUse() &&
688  Source->getArgOperand(1) == II->getArgOperand(1)) {
689  // If the source of the insert has only one use and it's another
690  // insert (and they're both inserting from the same vector), try to
691  // bundle both together.
692  auto CISourceWidth =
693  dyn_cast<ConstantInt>(Source->getArgOperand(2));
694  auto CISourceStart =
695  dyn_cast<ConstantInt>(Source->getArgOperand(3));
696  if (CISourceStart && CISourceWidth) {
697  unsigned Start = CIStart->getZExtValue();
698  unsigned Width = CIWidth->getZExtValue();
699  unsigned End = Start + Width;
700  unsigned SourceStart = CISourceStart->getZExtValue();
701  unsigned SourceWidth = CISourceWidth->getZExtValue();
702  unsigned SourceEnd = SourceStart + SourceWidth;
703  unsigned NewStart, NewWidth;
704  bool ShouldReplace = false;
705  if (Start <= SourceStart && SourceStart <= End) {
706  NewStart = Start;
707  NewWidth = std::max(End, SourceEnd) - NewStart;
708  ShouldReplace = true;
709  } else if (SourceStart <= Start && Start <= SourceEnd) {
710  NewStart = SourceStart;
711  NewWidth = std::max(SourceEnd, End) - NewStart;
712  ShouldReplace = true;
713  }
714 
715  if (ShouldReplace) {
716  Constant *ConstantWidth = ConstantInt::get(
717  II->getArgOperand(2)->getType(), NewWidth, false);
718  Constant *ConstantStart = ConstantInt::get(
719  II->getArgOperand(3)->getType(), NewStart, false);
720  Value *Args[4] = { Source->getArgOperand(0),
721  II->getArgOperand(1), ConstantWidth,
722  ConstantStart };
723  Module *M = CI.getParent()->getParent()->getParent();
724  Value *F =
725  Intrinsic::getDeclaration(M, Intrinsic::x86_sse4a_insertqi);
726  return ReplaceInstUsesWith(CI, Builder->CreateCall(F, Args));
727  }
728  }
729  }
730  }
731  }
732  }
733  break;
734  }
735 
736  case Intrinsic::x86_sse41_pblendvb:
737  case Intrinsic::x86_sse41_blendvps:
738  case Intrinsic::x86_sse41_blendvpd:
739  case Intrinsic::x86_avx_blendv_ps_256:
740  case Intrinsic::x86_avx_blendv_pd_256:
741  case Intrinsic::x86_avx2_pblendvb: {
742  // Convert blendv* to vector selects if the mask is constant.
743  // This optimization is convoluted because the intrinsic is defined as
744  // getting a vector of floats or doubles for the ps and pd versions.
745  // FIXME: That should be changed.
746  Value *Mask = II->getArgOperand(2);
747  if (auto C = dyn_cast<ConstantDataVector>(Mask)) {
748  auto Tyi1 = Builder->getInt1Ty();
749  auto SelectorType = cast<VectorType>(Mask->getType());
750  auto EltTy = SelectorType->getElementType();
751  unsigned Size = SelectorType->getNumElements();
752  unsigned BitWidth =
753  EltTy->isFloatTy()
754  ? 32
755  : (EltTy->isDoubleTy() ? 64 : EltTy->getIntegerBitWidth());
756  assert((BitWidth == 64 || BitWidth == 32 || BitWidth == 8) &&
757  "Wrong arguments for variable blend intrinsic");
758  SmallVector<Constant *, 32> Selectors;
759  for (unsigned I = 0; I < Size; ++I) {
760  // The intrinsics only read the top bit
761  uint64_t Selector;
762  if (BitWidth == 8)
763  Selector = C->getElementAsInteger(I);
764  else
765  Selector = C->getElementAsAPFloat(I).bitcastToAPInt().getZExtValue();
766  Selectors.push_back(ConstantInt::get(Tyi1, Selector >> (BitWidth - 1)));
767  }
768  auto NewSelector = ConstantVector::get(Selectors);
769  return SelectInst::Create(NewSelector, II->getArgOperand(1),
770  II->getArgOperand(0), "blendv");
771  } else {
772  break;
773  }
774  }
775 
776  case Intrinsic::x86_avx_vpermilvar_ps:
777  case Intrinsic::x86_avx_vpermilvar_ps_256:
778  case Intrinsic::x86_avx_vpermilvar_pd:
779  case Intrinsic::x86_avx_vpermilvar_pd_256: {
780  // Convert vpermil* to shufflevector if the mask is constant.
781  Value *V = II->getArgOperand(1);
782  unsigned Size = cast<VectorType>(V->getType())->getNumElements();
783  assert(Size == 8 || Size == 4 || Size == 2);
784  uint32_t Indexes[8];
785  if (auto C = dyn_cast<ConstantDataVector>(V)) {
786  // The intrinsics only read one or two bits, clear the rest.
787  for (unsigned I = 0; I < Size; ++I) {
788  uint32_t Index = C->getElementAsInteger(I) & 0x3;
789  if (II->getIntrinsicID() == Intrinsic::x86_avx_vpermilvar_pd ||
790  II->getIntrinsicID() == Intrinsic::x86_avx_vpermilvar_pd_256)
791  Index >>= 1;
792  Indexes[I] = Index;
793  }
794  } else if (isa<ConstantAggregateZero>(V)) {
795  for (unsigned I = 0; I < Size; ++I)
796  Indexes[I] = 0;
797  } else {
798  break;
799  }
800  // The _256 variants are a bit trickier since the mask bits always index
801  // into the corresponding 128 half. In order to convert to a generic
802  // shuffle, we have to make that explicit.
803  if (II->getIntrinsicID() == Intrinsic::x86_avx_vpermilvar_ps_256 ||
804  II->getIntrinsicID() == Intrinsic::x86_avx_vpermilvar_pd_256) {
805  for (unsigned I = Size / 2; I < Size; ++I)
806  Indexes[I] += Size / 2;
807  }
808  auto NewC =
809  ConstantDataVector::get(V->getContext(), makeArrayRef(Indexes, Size));
810  auto V1 = II->getArgOperand(0);
811  auto V2 = UndefValue::get(V1->getType());
812  auto Shuffle = Builder->CreateShuffleVector(V1, V2, NewC);
813  return ReplaceInstUsesWith(CI, Shuffle);
814  }
815 
816  case Intrinsic::ppc_altivec_vperm:
817  // Turn vperm(V1,V2,mask) -> shuffle(V1,V2,mask) if mask is a constant.
818  // Note that ppc_altivec_vperm has a big-endian bias, so when creating
819  // a vectorshuffle for little endian, we must undo the transformation
820  // performed on vec_perm in altivec.h. That is, we must complement
821  // the permutation mask with respect to 31 and reverse the order of
822  // V1 and V2.
823  if (Constant *Mask = dyn_cast<Constant>(II->getArgOperand(2))) {
824  assert(Mask->getType()->getVectorNumElements() == 16 &&
825  "Bad type for intrinsic!");
826 
827  // Check that all of the elements are integer constants or undefs.
828  bool AllEltsOk = true;
829  for (unsigned i = 0; i != 16; ++i) {
830  Constant *Elt = Mask->getAggregateElement(i);
831  if (!Elt || !(isa<ConstantInt>(Elt) || isa<UndefValue>(Elt))) {
832  AllEltsOk = false;
833  break;
834  }
835  }
836 
837  if (AllEltsOk) {
838  // Cast the input vectors to byte vectors.
839  Value *Op0 = Builder->CreateBitCast(II->getArgOperand(0),
840  Mask->getType());
841  Value *Op1 = Builder->CreateBitCast(II->getArgOperand(1),
842  Mask->getType());
843  Value *Result = UndefValue::get(Op0->getType());
844 
845  // Only extract each element once.
846  Value *ExtractedElts[32];
847  memset(ExtractedElts, 0, sizeof(ExtractedElts));
848 
849  for (unsigned i = 0; i != 16; ++i) {
850  if (isa<UndefValue>(Mask->getAggregateElement(i)))
851  continue;
852  unsigned Idx =
853  cast<ConstantInt>(Mask->getAggregateElement(i))->getZExtValue();
854  Idx &= 31; // Match the hardware behavior.
855  if (DL && DL->isLittleEndian())
856  Idx = 31 - Idx;
857 
858  if (!ExtractedElts[Idx]) {
859  Value *Op0ToUse = (DL && DL->isLittleEndian()) ? Op1 : Op0;
860  Value *Op1ToUse = (DL && DL->isLittleEndian()) ? Op0 : Op1;
861  ExtractedElts[Idx] =
862  Builder->CreateExtractElement(Idx < 16 ? Op0ToUse : Op1ToUse,
863  Builder->getInt32(Idx&15));
864  }
865 
866  // Insert this value into the result vector.
867  Result = Builder->CreateInsertElement(Result, ExtractedElts[Idx],
868  Builder->getInt32(i));
869  }
870  return CastInst::Create(Instruction::BitCast, Result, CI.getType());
871  }
872  }
873  break;
874 
875  case Intrinsic::arm_neon_vld1:
876  case Intrinsic::arm_neon_vld2:
877  case Intrinsic::arm_neon_vld3:
878  case Intrinsic::arm_neon_vld4:
879  case Intrinsic::arm_neon_vld2lane:
880  case Intrinsic::arm_neon_vld3lane:
881  case Intrinsic::arm_neon_vld4lane:
882  case Intrinsic::arm_neon_vst1:
883  case Intrinsic::arm_neon_vst2:
884  case Intrinsic::arm_neon_vst3:
885  case Intrinsic::arm_neon_vst4:
886  case Intrinsic::arm_neon_vst2lane:
887  case Intrinsic::arm_neon_vst3lane:
888  case Intrinsic::arm_neon_vst4lane: {
889  unsigned MemAlign = getKnownAlignment(II->getArgOperand(0), DL);
890  unsigned AlignArg = II->getNumArgOperands() - 1;
891  ConstantInt *IntrAlign = dyn_cast<ConstantInt>(II->getArgOperand(AlignArg));
892  if (IntrAlign && IntrAlign->getZExtValue() < MemAlign) {
893  II->setArgOperand(AlignArg,
895  MemAlign, false));
896  return II;
897  }
898  break;
899  }
900 
901  case Intrinsic::arm_neon_vmulls:
902  case Intrinsic::arm_neon_vmullu:
903  case Intrinsic::aarch64_neon_smull:
904  case Intrinsic::aarch64_neon_umull: {
905  Value *Arg0 = II->getArgOperand(0);
906  Value *Arg1 = II->getArgOperand(1);
907 
908  // Handle mul by zero first:
909  if (isa<ConstantAggregateZero>(Arg0) || isa<ConstantAggregateZero>(Arg1)) {
911  }
912 
913  // Check for constant LHS & RHS - in this case we just simplify.
914  bool Zext = (II->getIntrinsicID() == Intrinsic::arm_neon_vmullu ||
915  II->getIntrinsicID() == Intrinsic::aarch64_neon_umull);
916  VectorType *NewVT = cast<VectorType>(II->getType());
917  if (Constant *CV0 = dyn_cast<Constant>(Arg0)) {
918  if (Constant *CV1 = dyn_cast<Constant>(Arg1)) {
919  CV0 = ConstantExpr::getIntegerCast(CV0, NewVT, /*isSigned=*/!Zext);
920  CV1 = ConstantExpr::getIntegerCast(CV1, NewVT, /*isSigned=*/!Zext);
921 
922  return ReplaceInstUsesWith(CI, ConstantExpr::getMul(CV0, CV1));
923  }
924 
925  // Couldn't simplify - canonicalize constant to the RHS.
926  std::swap(Arg0, Arg1);
927  }
928 
929  // Handle mul by one:
930  if (Constant *CV1 = dyn_cast<Constant>(Arg1))
931  if (ConstantInt *Splat =
932  dyn_cast_or_null<ConstantInt>(CV1->getSplatValue()))
933  if (Splat->isOne())
934  return CastInst::CreateIntegerCast(Arg0, II->getType(),
935  /*isSigned=*/!Zext);
936 
937  break;
938  }
939 
940  case Intrinsic::AMDGPU_rcp: {
941  if (const ConstantFP *C = dyn_cast<ConstantFP>(II->getArgOperand(0))) {
942  const APFloat &ArgVal = C->getValueAPF();
943  APFloat Val(ArgVal.getSemantics(), 1.0);
944  APFloat::opStatus Status = Val.divide(ArgVal,
946  // Only do this if it was exact and therefore not dependent on the
947  // rounding mode.
948  if (Status == APFloat::opOK)
949  return ReplaceInstUsesWith(CI, ConstantFP::get(II->getContext(), Val));
950  }
951 
952  break;
953  }
954  case Intrinsic::stackrestore: {
955  // If the save is right next to the restore, remove the restore. This can
956  // happen when variable allocas are DCE'd.
957  if (IntrinsicInst *SS = dyn_cast<IntrinsicInst>(II->getArgOperand(0))) {
958  if (SS->getIntrinsicID() == Intrinsic::stacksave) {
959  BasicBlock::iterator BI = SS;
960  if (&*++BI == II)
961  return EraseInstFromFunction(CI);
962  }
963  }
964 
965  // Scan down this block to see if there is another stack restore in the
966  // same block without an intervening call/alloca.
967  BasicBlock::iterator BI = II;
968  TerminatorInst *TI = II->getParent()->getTerminator();
969  bool CannotRemove = false;
970  for (++BI; &*BI != TI; ++BI) {
971  if (isa<AllocaInst>(BI)) {
972  CannotRemove = true;
973  break;
974  }
975  if (CallInst *BCI = dyn_cast<CallInst>(BI)) {
976  if (IntrinsicInst *II = dyn_cast<IntrinsicInst>(BCI)) {
977  // If there is a stackrestore below this one, remove this one.
978  if (II->getIntrinsicID() == Intrinsic::stackrestore)
979  return EraseInstFromFunction(CI);
980  // Otherwise, ignore the intrinsic.
981  } else {
982  // If we found a non-intrinsic call, we can't remove the stack
983  // restore.
984  CannotRemove = true;
985  break;
986  }
987  }
988  }
989 
990  // If the stack restore is in a return, resume, or unwind block and if there
991  // are no allocas or calls between the restore and the return, nuke the
992  // restore.
993  if (!CannotRemove && (isa<ReturnInst>(TI) || isa<ResumeInst>(TI)))
994  return EraseInstFromFunction(CI);
995  break;
996  }
997  }
998 
999  return visitCallSite(II);
1000 }
Value * CreateLShr(Value *LHS, Value *RHS, const Twine &Name="", bool isExact=false)
Definition: IRBuilder.h:770
static ConstantInt * getFalse(LLVMContext &Context)
Definition: Constants.cpp:481
void setDoesNotThrow()
class_match< Value > m_Value()
m_Value() - Match an arbitrary value and ignore it.
Definition: PatternMatch.h:70
void *memcpy(void *s1, const void *s2, size_t n);
The main container class for the LLVM Intermediate Representation.
Definition: Module.h:115
Intrinsic::ID getIntrinsicID() const
Definition: IntrinsicInst.h:43
bool WillNotOverflowSignedAdd(Value *LHS, Value *RHS)
unsigned getOrEnforceKnownAlignment(Value *V, unsigned PrefAlign, const DataLayout *TD=nullptr)
Value * SimplifyDemandedVectorElts(Value *V, APInt DemandedElts, APInt &UndefElts, unsigned Depth=0)
static ConstantAggregateZero * get(Type *Ty)
Definition: Constants.cpp:1269
static APInt getLowBitsSet(unsigned numBits, unsigned loBitsSet)
Get a value with low bits set.
Definition: APInt.h:526
m_Intrinsic_Ty< Opnd0 >::Ty m_BSwap(const Opnd0 &Op0)
static RegisterPass< NVPTXAllocaHoisting > X("alloca-hoisting","Hoisting alloca instructions in non-entry ""blocks to the entry block")
const Function * getParent() const
Return the enclosing method, or null if none.
Definition: BasicBlock.h:118
Value * CreateNUWMul(Value *LHS, Value *RHS, const Twine &Name="")
Definition: IRBuilder.h:687
F(f)
unsigned getBitWidth() const
Get the number of bits in this IntegerType.
Definition: DerivedTypes.h:61
Value * CreateShuffleVector(Value *V1, Value *V2, Value *Mask, const Twine &Name="")
Definition: IRBuilder.h:1407
Value * CreateInsertElement(Value *Vec, Value *NewElt, Value *Idx, const Twine &Name="")
Definition: IRBuilder.h:1398
const CallInst * isFreeCall(const Value *I, const TargetLibraryInfo *TLI)
isFreeCall - Returns non-null if the value is a call to the builtin free()
static Constant * getNullValue(Type *Ty)
Definition: Constants.cpp:133
Value * CreateNSWAdd(Value *LHS, Value *RHS, const Twine &Name="")
Definition: IRBuilder.h:640
Instruction * SimplifyMemSet(MemSetInst *MI)
bool match(Val *V, const Pattern &P)
Definition: PatternMatch.h:42
TargetLibraryInfo * TLI
Definition: InstCombine.h:90
static Constant * getIntegerCast(Constant *C, Type *Ty, bool isSigned)
Create a ZExt, Bitcast or Trunc for integer -> integer casts.
Definition: Constants.cpp:1562
static unsigned getBitWidth(Type *Ty, const DataLayout *TD)
bool doesNotThrow() const
Determine if the function cannot unwind.
Definition: Function.h:271
static cl::opt< ITMode > IT(cl::desc("IT block support"), cl::Hidden, cl::init(DefaultIT), cl::ZeroOrMore, cl::values(clEnumValN(DefaultIT,"arm-default-it","Generate IT block based on arch"), clEnumValN(RestrictedIT,"arm-restrict-it","Disallow deprecated IT based on ARMv8"), clEnumValN(NoRestrictedIT,"arm-no-restrict-it","Allow IT blocks based on ARMv7"), clEnumValEnd))
ArrayRef< T > makeArrayRef(const T &OneElt)
Construct an ArrayRef from a single element.
Definition: ArrayRef.h:276
unsigned getNumArgOperands() const
CastClass_match< OpTy, Instruction::Trunc > m_Trunc(const OpTy &Op)
m_Trunc
Definition: PatternMatch.h:768
static Constant * get(ArrayRef< Constant * > V)
Definition: Constants.cpp:961
bool doesNotThrow() const
Determine if the call cannot unwind.
uint64_t getZExtValue() const
Return the zero extended value.
Definition: Constants.h:116
static SelectInst * Create(Value *C, Value *S1, Value *S2, const Twine &NameStr="", Instruction *InsertBefore=nullptr)
Function * getDeclaration(Module *M, ID id, ArrayRef< Type * > Tys=None)
Definition: Function.cpp:737
APInt umul_ov(const APInt &RHS, bool &Overflow) const
Bitwise AND operator.
Definition: APInt.cpp:2039
void takeName(Value *V)
Definition: Value.cpp:242
This class represents a truncation of integer types.
static APInt getHighBitsSet(unsigned numBits, unsigned hiBitsSet)
Get a value with high bits set.
Definition: APInt.h:508
A self-contained host- and target-independent arbitrary-precision floating-point software implementat...
Definition: APFloat.h:122
assert(Globals.size() > 1)
Value * CreateAdd(Value *LHS, Value *RHS, const Twine &Name="", bool HasNUW=false, bool HasNSW=false)
Definition: IRBuilder.h:632
LLVM Constant Representation.
Definition: Constant.h:41
Instruction * ReplaceInstUsesWith(Instruction &I, Value *V)
Definition: InstCombine.h:282
bool isFloatTy() const
isFloatTy - Return true if this is 'float', a 32-bit IEEE fp type.
Definition: Type.h:143
Instruction * visitCallSite(CallSite CS)
Integer representation type.
Definition: DerivedTypes.h:37
static Constant * get(StructType *T, ArrayRef< Constant * > V)
Definition: Constants.cpp:912
BuilderTy * Builder
Definition: InstCombine.h:102
static UndefValue * get(Type *T)
Definition: Constants.cpp:1372
LLVMContext & getContext() const
All values hold a context through their type.
Definition: Value.cpp:615
static CastInst * CreateIntegerCast(Value *S, Type *Ty, bool isSigned, const Twine &Name="", Instruction *InsertBefore=nullptr)
Create a ZExt, BitCast, or Trunc for int -> int casts.
Value * CreateNUWAdd(Value *LHS, Value *RHS, const Twine &Name="")
Definition: IRBuilder.h:643
static PointerType * getUnqual(Type *ElementType)
Definition: DerivedTypes.h:456
Class for constant integers.
Definition: Constants.h:51
const char * Args[]
Value * CreateExtractElement(Value *Vec, Value *Idx, const Twine &Name="")
Definition: IRBuilder.h:1390
unsigned getVectorNumElements() const
Definition: Type.cpp:208
Instruction * SimplifyMemTransfer(MemIntrinsic *MI)
Value * CreateBitCast(Value *V, Type *DestTy, const Twine &Name="")
Definition: IRBuilder.h:1158
Type * getType() const
Definition: Value.h:215
ConstantInt * getInt32(uint32_t C)
Get a constant 32-bit value.
Definition: IRBuilder.h:276
void *memset(void *b, int c, size_t len);
Instruction * visitFree(CallInst &FI)
static Constant * get(Type *Ty, uint64_t V, bool isSigned=false)
Definition: Constants.cpp:528
static Constant * get(Type *Ty, double V)
Definition: Constants.cpp:593
static ConstantInt * getTrue(LLVMContext &Context)
Definition: Constants.cpp:474
void swap(llvm::BitVector &LHS, llvm::BitVector &RHS)
Implement std::swap in terms of BitVector swap.
Definition: BitVector.h:590
Value * getArgOperand(unsigned i) const
Class for arbitrary precision integers.
Definition: APInt.h:75
static unsigned getKnownAlignment(Value *V, const DataLayout *TD=nullptr)
getKnownAlignment - Try to infer an alignment for the specified pointer.
Definition: Local.h:176
LLVM_ATTRIBUTE_UNUSED_RESULT std::enable_if< !is_simple_type< Y >::value, typename cast_retty< X, const Y >::ret_type >::type dyn_cast(const Y &Val)
Definition: Casting.h:265
Value * CreateVectorSplat(unsigned NumElts, Value *V, const Twine &Name="")
Return a vector value that contains.
Definition: IRBuilder.h:1474
static APInt getBitsSet(unsigned numBits, unsigned loBit, unsigned hiBit)
Get a value with a block of bits set.
Definition: APInt.h:493
ConstantInt * getFalse()
Get the constant value for i1 false.
Definition: IRBuilder.h:261
IntegerType * getInt1Ty()
Fetch the type representing a single bit.
Definition: IRBuilder.h:301
void Shuffle(internal::Random *random, std::vector< E > *v)
static CastInst * Create(Instruction::CastOps, Value *S, Type *Ty, const Twine &Name="", Instruction *InsertBefore=nullptr)
Construct any of the CastInst subclasses.
void setCalledFunction(Value *Fn)
setCalledFunction - Set the function called.
static IntegerType * getInt32Ty(LLVMContext &C)
Definition: Type.cpp:235
static InsertValueInst * Create(Value *Agg, Value *Val, ArrayRef< unsigned > Idxs, const Twine &NameStr="", Instruction *InsertBefore=nullptr)
#define I(x, y, z)
Definition: MD5.cpp:54
TerminatorInst * getTerminator()
Returns the terminator instruction if the block is well formed or null if the block is not well forme...
Definition: BasicBlock.cpp:124
void setArgOperand(unsigned i, Value *v)
const DataLayout * DL
Definition: InstCombine.h:89
unsigned getPrimitiveSizeInBits() const LLVM_READONLY
Definition: Type.cpp:117
Module * getParent()
Get the module that this global value is contained inside of...
Definition: GlobalValue.h:336
LLVM Value Representation.
Definition: Value.h:69
CallInst * CreateCall(Value *Callee, const Twine &Name="")
Definition: IRBuilder.h:1345
Instruction * EraseInstFromFunction(Instruction &I)
Definition: InstCombine.h:301
void computeKnownBits(Value *V, APInt &KnownZero, APInt &KnownOne, unsigned Depth=0) const
Definition: InstCombine.h:318
bool getObjectSize(const Value *Ptr, uint64_t &Size, const DataLayout *DL, const TargetLibraryInfo *TLI, bool RoundToAlign=false)
Compute the size of the object pointed by Ptr. Returns true and the object size in Size if successful...
static Constant * getMul(Constant *C1, Constant *C2, bool HasNUW=false, bool HasNSW=false)
Definition: Constants.cpp:2124
static Constant * get(LLVMContext &Context, ArrayRef< uint8_t > Elts)
Definition: Constants.cpp:2447
const fltSemantics & getSemantics() const
Definition: APFloat.h:398
const BasicBlock * getParent() const
Definition: Instruction.h:53
Instruction * InstCombiner::visitCallSite ( CallSite  CS)
private

Definition at line 1126 of file InstCombineCalls.cpp.

1126  {
1127  if (isAllocLikeFn(CS.getInstruction(), TLI))
1128  return visitAllocSite(*CS.getInstruction());
1129 
1130  bool Changed = false;
1131 
1132  // If the callee is a pointer to a function, attempt to move any casts to the
1133  // arguments of the call/invoke.
1134  Value *Callee = CS.getCalledValue();
1135  if (!isa<Function>(Callee) && transformConstExprCastCall(CS))
1136  return nullptr;
1137 
1138  if (Function *CalleeF = dyn_cast<Function>(Callee))
1139  // If the call and callee calling conventions don't match, this call must
1140  // be unreachable, as the call is undefined.
1141  if (CalleeF->getCallingConv() != CS.getCallingConv() &&
1142  // Only do this for calls to a function with a body. A prototype may
1143  // not actually end up matching the implementation's calling conv for a
1144  // variety of reasons (e.g. it may be written in assembly).
1145  !CalleeF->isDeclaration()) {
1146  Instruction *OldCall = CS.getInstruction();
1147  new StoreInst(ConstantInt::getTrue(Callee->getContext()),
1149  OldCall);
1150  // If OldCall does not return void then replaceAllUsesWith undef.
1151  // This allows ValueHandlers and custom metadata to adjust itself.
1152  if (!OldCall->getType()->isVoidTy())
1153  ReplaceInstUsesWith(*OldCall, UndefValue::get(OldCall->getType()));
1154  if (isa<CallInst>(OldCall))
1155  return EraseInstFromFunction(*OldCall);
1156 
1157  // We cannot remove an invoke, because it would change the CFG, just
1158  // change the callee to a null pointer.
1159  cast<InvokeInst>(OldCall)->setCalledFunction(
1160  Constant::getNullValue(CalleeF->getType()));
1161  return nullptr;
1162  }
1163 
1164  if (isa<ConstantPointerNull>(Callee) || isa<UndefValue>(Callee)) {
1165  // If CS does not return void then replaceAllUsesWith undef.
1166  // This allows ValueHandlers and custom metadata to adjust itself.
1167  if (!CS.getInstruction()->getType()->isVoidTy())
1170 
1171  if (isa<InvokeInst>(CS.getInstruction())) {
1172  // Can't remove an invoke because we cannot change the CFG.
1173  return nullptr;
1174  }
1175 
1176  // This instruction is not reachable, just remove it. We insert a store to
1177  // undef so that we know that this code is not reachable, despite the fact
1178  // that we can't modify the CFG here.
1179  new StoreInst(ConstantInt::getTrue(Callee->getContext()),
1181  CS.getInstruction());
1182 
1183  return EraseInstFromFunction(*CS.getInstruction());
1184  }
1185 
1186  if (IntrinsicInst *II = FindInitTrampoline(Callee))
1187  return transformCallThroughTrampoline(CS, II);
1188 
1189  PointerType *PTy = cast<PointerType>(Callee->getType());
1190  FunctionType *FTy = cast<FunctionType>(PTy->getElementType());
1191  if (FTy->isVarArg()) {
1192  int ix = FTy->getNumParams();
1193  // See if we can optimize any arguments passed through the varargs area of
1194  // the call.
1195  for (CallSite::arg_iterator I = CS.arg_begin() + FTy->getNumParams(),
1196  E = CS.arg_end(); I != E; ++I, ++ix) {
1197  CastInst *CI = dyn_cast<CastInst>(*I);
1198  if (CI && isSafeToEliminateVarargsCast(CS, CI, DL, ix)) {
1199  *I = CI->getOperand(0);
1200  Changed = true;
1201  }
1202  }
1203  }
1204 
1205  if (isa<InlineAsm>(Callee) && !CS.doesNotThrow()) {
1206  // Inline asm calls cannot throw - mark them 'nounwind'.
1207  CS.setDoesNotThrow();
1208  Changed = true;
1209  }
1210 
1211  // Try to optimize the call if possible, we require DataLayout for most of
1212  // this. None of these calls are seen as possibly dead so go ahead and
1213  // delete the instruction now.
1214  if (CallInst *CI = dyn_cast<CallInst>(CS.getInstruction())) {
1215  Instruction *I = tryOptimizeCall(CI, DL);
1216  // If we changed something return the result, etc. Otherwise let
1217  // the fallthrough check.
1218  if (I) return EraseInstFromFunction(*I);
1219  }
1220 
1221  return Changed ? CS.getInstruction() : nullptr;
1222 }
unsigned getNumParams() const
Definition: DerivedTypes.h:133
IterTy arg_end() const
Definition: CallSite.h:151
Instruction * transformCallThroughTrampoline(CallSite CS, IntrinsicInst *Tramp)
static Constant * getNullValue(Type *Ty)
Definition: Constants.cpp:133
TargetLibraryInfo * TLI
Definition: InstCombine.h:90
Instruction * tryOptimizeCall(CallInst *CI, const DataLayout *DL)
Base class of casting instructions.
Definition: InstrTypes.h:387
bool transformConstExprCastCall(CallSite CS)
static IntrinsicInst * FindInitTrampoline(Value *Callee)
CallingConv::ID getCallingConv() const
Definition: CallSite.h:189
ValTy * getCalledValue() const
Definition: CallSite.h:85
Type * getElementType() const
Definition: DerivedTypes.h:319
static bool isSafeToEliminateVarargsCast(const CallSite CS, const CastInst *const CI, const DataLayout *const DL, const int ix)
InstrTy * getInstruction() const
Definition: CallSite.h:79
Instruction * ReplaceInstUsesWith(Instruction &I, Value *V)
Definition: InstCombine.h:282
Value * getOperand(unsigned i) const
Definition: User.h:90
static UndefValue * get(Type *T)
Definition: Constants.cpp:1372
LLVMContext & getContext() const
All values hold a context through their type.
Definition: Value.cpp:615
static PointerType * getInt1PtrTy(LLVMContext &C, unsigned AS=0)
Definition: Type.cpp:274
bool doesNotThrow() const
Determine if the call cannot unwind.
Definition: CallSite.h:265
Type * getType() const
Definition: Value.h:215
static ConstantInt * getTrue(LLVMContext &Context)
Definition: Constants.cpp:474
LLVM_ATTRIBUTE_UNUSED_RESULT std::enable_if< !is_simple_type< Y >::value, typename cast_retty< X, const Y >::ret_type >::type dyn_cast(const Y &Val)
Definition: Casting.h:265
void setDoesNotThrow()
Definition: CallSite.h:268
#define I(x, y, z)
Definition: MD5.cpp:54
bool isAllocLikeFn(const Value *V, const TargetLibraryInfo *TLI, bool LookThroughBitCast=false)
Tests if a value is a call or invoke to a library function that allocates memory (either malloc...
IterTy arg_begin() const
Definition: CallSite.h:145
const DataLayout * DL
Definition: InstCombine.h:89
LLVM Value Representation.
Definition: Value.h:69
Instruction * EraseInstFromFunction(Instruction &I)
Definition: InstCombine.h:301
Instruction * visitAllocSite(Instruction &FI)
bool isVoidTy() const
isVoidTy - Return true if this is 'void'.
Definition: Type.h:137
Instruction * llvm::InstVisitor< InstCombiner , Instruction * >::visitCastInst ( CastInst I)
inlineinherited

Definition at line 231 of file InstVisitor.h.

231 { DELEGATE(UnaryInstruction);}
#define DELEGATE(CLASS_TO_VISIT)
Definition: InstVisitor.h:30
Instruction * llvm::InstVisitor< InstCombiner , Instruction * >::visitCmpInst ( CmpInst I)
inlineinherited

Definition at line 233 of file InstVisitor.h.

#define DELEGATE(CLASS_TO_VISIT)
Definition: InstVisitor.h:30
Instruction * llvm::InstVisitor< InstCombiner , Instruction * >::visitDbgDeclareInst ( DbgDeclareInst I)
inlineinherited

Definition at line 205 of file InstVisitor.h.

205 { DELEGATE(DbgInfoIntrinsic);}
#define DELEGATE(CLASS_TO_VISIT)
Definition: InstVisitor.h:30
Instruction * llvm::InstVisitor< InstCombiner , Instruction * >::visitDbgInfoIntrinsic ( DbgInfoIntrinsic I)
inlineinherited

Definition at line 207 of file InstVisitor.h.

207 { DELEGATE(IntrinsicInst); }
#define DELEGATE(CLASS_TO_VISIT)
Definition: InstVisitor.h:30
Instruction * llvm::InstVisitor< InstCombiner , Instruction * >::visitDbgValueInst ( DbgValueInst I)
inlineinherited

Definition at line 206 of file InstVisitor.h.

206 { DELEGATE(DbgInfoIntrinsic);}
#define DELEGATE(CLASS_TO_VISIT)
Definition: InstVisitor.h:30
Instruction * InstCombiner::visitExtractElementInst ( ExtractElementInst EI)

Definition at line 179 of file InstCombineVectorOps.cpp.

179  {
180  // If vector val is constant with all elements the same, replace EI with
181  // that element. We handle a known element # below.
182  if (Constant *C = dyn_cast<Constant>(EI.getOperand(0)))
183  if (CheapToScalarize(C, false))
184  return ReplaceInstUsesWith(EI, C->getAggregateElement(0U));
185 
186  // If extracting a specified index from the vector, see if we can recursively
187  // find a previously computed scalar that was inserted into the vector.
188  if (ConstantInt *IdxC = dyn_cast<ConstantInt>(EI.getOperand(1))) {
189  unsigned IndexVal = IdxC->getZExtValue();
190  unsigned VectorWidth = EI.getVectorOperandType()->getNumElements();
191 
192  // If this is extracting an invalid index, turn this into undef, to avoid
193  // crashing the code below.
194  if (IndexVal >= VectorWidth)
195  return ReplaceInstUsesWith(EI, UndefValue::get(EI.getType()));
196 
197  // This instruction only demands the single element from the input vector.
198  // If the input vector has a single use, simplify it based on this use
199  // property.
200  if (EI.getOperand(0)->hasOneUse() && VectorWidth != 1) {
201  APInt UndefElts(VectorWidth, 0);
202  APInt DemandedMask(VectorWidth, 0);
203  DemandedMask.setBit(IndexVal);
205  DemandedMask, UndefElts)) {
206  EI.setOperand(0, V);
207  return &EI;
208  }
209  }
210 
211  if (Value *Elt = FindScalarElement(EI.getOperand(0), IndexVal))
212  return ReplaceInstUsesWith(EI, Elt);
213 
214  // If the this extractelement is directly using a bitcast from a vector of
215  // the same number of elements, see if we can find the source element from
216  // it. In this case, we will end up needing to bitcast the scalars.
217  if (BitCastInst *BCI = dyn_cast<BitCastInst>(EI.getOperand(0))) {
218  if (VectorType *VT = dyn_cast<VectorType>(BCI->getOperand(0)->getType()))
219  if (VT->getNumElements() == VectorWidth)
220  if (Value *Elt = FindScalarElement(BCI->getOperand(0), IndexVal))
221  return new BitCastInst(Elt, EI.getType());
222  }
223 
224  // If there's a vector PHI feeding a scalar use through this extractelement
225  // instruction, try to scalarize the PHI.
226  if (PHINode *PN = dyn_cast<PHINode>(EI.getOperand(0))) {
227  Instruction *scalarPHI = scalarizePHI(EI, PN);
228  if (scalarPHI)
229  return scalarPHI;
230  }
231  }
232 
233  if (Instruction *I = dyn_cast<Instruction>(EI.getOperand(0))) {
234  // Push extractelement into predecessor operation if legal and
235  // profitable to do so
236  if (BinaryOperator *BO = dyn_cast<BinaryOperator>(I)) {
237  if (I->hasOneUse() &&
238  CheapToScalarize(BO, isa<ConstantInt>(EI.getOperand(1)))) {
239  Value *newEI0 =
240  Builder->CreateExtractElement(BO->getOperand(0), EI.getOperand(1),
241  EI.getName()+".lhs");
242  Value *newEI1 =
243  Builder->CreateExtractElement(BO->getOperand(1), EI.getOperand(1),
244  EI.getName()+".rhs");
245  return BinaryOperator::Create(BO->getOpcode(), newEI0, newEI1);
246  }
247  } else if (InsertElementInst *IE = dyn_cast<InsertElementInst>(I)) {
248  // Extracting the inserted element?
249  if (IE->getOperand(2) == EI.getOperand(1))
250  return ReplaceInstUsesWith(EI, IE->getOperand(1));
251  // If the inserted and extracted elements are constants, they must not
252  // be the same value, extract from the pre-inserted value instead.
253  if (isa<Constant>(IE->getOperand(2)) && isa<Constant>(EI.getOperand(1))) {
255  EI.setOperand(0, IE->getOperand(0));
256  return &EI;
257  }
258  } else if (ShuffleVectorInst *SVI = dyn_cast<ShuffleVectorInst>(I)) {
259  // If this is extracting an element from a shufflevector, figure out where
260  // it came from and extract from the appropriate input element instead.
261  if (ConstantInt *Elt = dyn_cast<ConstantInt>(EI.getOperand(1))) {
262  int SrcIdx = SVI->getMaskValue(Elt->getZExtValue());
263  Value *Src;
264  unsigned LHSWidth =
265  SVI->getOperand(0)->getType()->getVectorNumElements();
266 
267  if (SrcIdx < 0)
268  return ReplaceInstUsesWith(EI, UndefValue::get(EI.getType()));
269  if (SrcIdx < (int)LHSWidth)
270  Src = SVI->getOperand(0);
271  else {
272  SrcIdx -= LHSWidth;
273  Src = SVI->getOperand(1);
274  }
276  return ExtractElementInst::Create(Src,
277  ConstantInt::get(Int32Ty,
278  SrcIdx, false));
279  }
280  } else if (CastInst *CI = dyn_cast<CastInst>(I)) {
281  // Canonicalize extractelement(cast) -> cast(extractelement)
282  // bitcasts can change the number of vector elements and they cost nothing
283  if (CI->hasOneUse() && (CI->getOpcode() != Instruction::BitCast)) {
284  Value *EE = Builder->CreateExtractElement(CI->getOperand(0),
285  EI.getIndexOperand());
286  Worklist.AddValue(EE);
287  return CastInst::Create(CI->getOpcode(), EE, EI.getType());
288  }
289  } else if (SelectInst *SI = dyn_cast<SelectInst>(I)) {
290  if (SI->hasOneUse()) {
291  // TODO: For a select on vectors, it might be useful to do this if it
292  // has multiple extractelement uses. For vector select, that seems to
293  // fight the vectorizer.
294 
295  // If we are extracting an element from a vector select or a select on
296  // vectors, a select on the scalars extracted from the vector arguments.
297  Value *TrueVal = SI->getTrueValue();
298  Value *FalseVal = SI->getFalseValue();
299 
300  Value *Cond = SI->getCondition();
301  if (Cond->getType()->isVectorTy()) {
302  Cond = Builder->CreateExtractElement(Cond,
303  EI.getIndexOperand(),
304  Cond->getName() + ".elt");
305  }
306 
307  Value *V1Elem
308  = Builder->CreateExtractElement(TrueVal,
309  EI.getIndexOperand(),
310  TrueVal->getName() + ".elt");
311 
312  Value *V2Elem
313  = Builder->CreateExtractElement(FalseVal,
314  EI.getIndexOperand(),
315  FalseVal->getName() + ".elt");
316  return SelectInst::Create(Cond,
317  V1Elem,
318  V2Elem,
319  SI->getName() + ".elt");
320  }
321  }
322  }
323  return nullptr;
324 }
Value * SimplifyDemandedVectorElts(Value *V, APInt DemandedElts, APInt &UndefElts, unsigned Depth=0)
StringRef getName() const
Definition: Value.cpp:168
Base class of casting instructions.
Definition: InstrTypes.h:387
static Value * FindScalarElement(Value *V, unsigned EltNo)
VectorType * getVectorOperandType() const
static SelectInst * Create(Value *C, Value *S1, Value *S2, const Twine &NameStr="", Instruction *InsertBefore=nullptr)
This class represents a no-op cast from one type to another.
unsigned getNumElements() const
Return the number of elements in the Vector type.
Definition: DerivedTypes.h:428
Type * Int32Ty
bool isVectorTy() const
Definition: Type.h:226
LLVM Constant Representation.
Definition: Constant.h:41
Instruction * ReplaceInstUsesWith(Instruction &I, Value *V)
Definition: InstCombine.h:282
static bool CheapToScalarize(Value *V, bool isConstant)
Value * getOperand(unsigned i) const
Definition: User.h:90
InstCombineWorklist Worklist
Worklist - All of the instructions that need to be simplified.
Definition: InstCombine.h:97
BuilderTy * Builder
Definition: InstCombine.h:102
static UndefValue * get(Type *T)
Definition: Constants.cpp:1372
LLVMContext & getContext() const
All values hold a context through their type.
Definition: Value.cpp:615
Instruction * scalarizePHI(ExtractElementInst &EI, PHINode *PN)
Class for constant integers.
Definition: Constants.h:51
Value * CreateExtractElement(Value *Vec, Value *Idx, const Twine &Name="")
Definition: IRBuilder.h:1390
unsigned getVectorNumElements() const
Definition: Type.cpp:208
Type * getType() const
Definition: Value.h:215
static Constant * get(Type *Ty, uint64_t V, bool isSigned=false)
Definition: Constants.cpp:528
void setOperand(unsigned i, Value *Val)
Definition: User.h:94
Class for arbitrary precision integers.
Definition: APInt.h:75
static BinaryOperator * Create(BinaryOps Op, Value *S1, Value *S2, const Twine &Name=Twine(), Instruction *InsertBefore=nullptr)
static CastInst * Create(Instruction::CastOps, Value *S, Type *Ty, const Twine &Name="", Instruction *InsertBefore=nullptr)
Construct any of the CastInst subclasses.
static IntegerType * getInt32Ty(LLVMContext &C)
Definition: Type.cpp:235
#define I(x, y, z)
Definition: MD5.cpp:54
bool hasOneUse() const
Definition: Value.h:284
LLVM Value Representation.
Definition: Value.h:69
static ExecutionEngine * EE
Definition: lli.cpp:323
static ExtractElementInst * Create(Value *Vec, Value *Idx, const Twine &NameStr="", Instruction *InsertBefore=nullptr)
Instruction * InstCombiner::visitExtractValueInst ( ExtractValueInst EV)

Definition at line 1999 of file InstructionCombining.cpp.

1999  {
2000  Value *Agg = EV.getAggregateOperand();
2001 
2002  if (!EV.hasIndices())
2003  return ReplaceInstUsesWith(EV, Agg);
2004 
2005  if (Constant *C = dyn_cast<Constant>(Agg)) {
2006  if (Constant *C2 = C->getAggregateElement(*EV.idx_begin())) {
2007  if (EV.getNumIndices() == 0)
2008  return ReplaceInstUsesWith(EV, C2);
2009  // Extract the remaining indices out of the constant indexed by the
2010  // first index
2011  return ExtractValueInst::Create(C2, EV.getIndices().slice(1));
2012  }
2013  return nullptr; // Can't handle other constants
2014  }
2015 
2016  if (InsertValueInst *IV = dyn_cast<InsertValueInst>(Agg)) {
2017  // We're extracting from an insertvalue instruction, compare the indices
2018  const unsigned *exti, *exte, *insi, *inse;
2019  for (exti = EV.idx_begin(), insi = IV->idx_begin(),
2020  exte = EV.idx_end(), inse = IV->idx_end();
2021  exti != exte && insi != inse;
2022  ++exti, ++insi) {
2023  if (*insi != *exti)
2024  // The insert and extract both reference distinctly different elements.
2025  // This means the extract is not influenced by the insert, and we can
2026  // replace the aggregate operand of the extract with the aggregate
2027  // operand of the insert. i.e., replace
2028  // %I = insertvalue { i32, { i32 } } %A, { i32 } { i32 42 }, 1
2029  // %E = extractvalue { i32, { i32 } } %I, 0
2030  // with
2031  // %E = extractvalue { i32, { i32 } } %A, 0
2032  return ExtractValueInst::Create(IV->getAggregateOperand(),
2033  EV.getIndices());
2034  }
2035  if (exti == exte && insi == inse)
2036  // Both iterators are at the end: Index lists are identical. Replace
2037  // %B = insertvalue { i32, { i32 } } %A, i32 42, 1, 0
2038  // %C = extractvalue { i32, { i32 } } %B, 1, 0
2039  // with "i32 42"
2040  return ReplaceInstUsesWith(EV, IV->getInsertedValueOperand());
2041  if (exti == exte) {
2042  // The extract list is a prefix of the insert list. i.e. replace
2043  // %I = insertvalue { i32, { i32 } } %A, i32 42, 1, 0
2044  // %E = extractvalue { i32, { i32 } } %I, 1
2045  // with
2046  // %X = extractvalue { i32, { i32 } } %A, 1
2047  // %E = insertvalue { i32 } %X, i32 42, 0
2048  // by switching the order of the insert and extract (though the
2049  // insertvalue should be left in, since it may have other uses).
2050  Value *NewEV = Builder->CreateExtractValue(IV->getAggregateOperand(),
2051  EV.getIndices());
2052  return InsertValueInst::Create(NewEV, IV->getInsertedValueOperand(),
2053  makeArrayRef(insi, inse));
2054  }
2055  if (insi == inse)
2056  // The insert list is a prefix of the extract list
2057  // We can simply remove the common indices from the extract and make it
2058  // operate on the inserted value instead of the insertvalue result.
2059  // i.e., replace
2060  // %I = insertvalue { i32, { i32 } } %A, { i32 } { i32 42 }, 1
2061  // %E = extractvalue { i32, { i32 } } %I, 1, 0
2062  // with
2063  // %E extractvalue { i32 } { i32 42 }, 0
2064  return ExtractValueInst::Create(IV->getInsertedValueOperand(),
2065  makeArrayRef(exti, exte));
2066  }
2067  if (IntrinsicInst *II = dyn_cast<IntrinsicInst>(Agg)) {
2068  // We're extracting from an intrinsic, see if we're the only user, which
2069  // allows us to simplify multiple result intrinsics to simpler things that
2070  // just get one value.
2071  if (II->hasOneUse()) {
2072  // Check if we're grabbing the overflow bit or the result of a 'with
2073  // overflow' intrinsic. If it's the latter we can remove the intrinsic
2074  // and replace it with a traditional binary instruction.
2075  switch (II->getIntrinsicID()) {
2076  case Intrinsic::uadd_with_overflow:
2077  case Intrinsic::sadd_with_overflow:
2078  if (*EV.idx_begin() == 0) { // Normal result.
2079  Value *LHS = II->getArgOperand(0), *RHS = II->getArgOperand(1);
2080  ReplaceInstUsesWith(*II, UndefValue::get(II->getType()));
2081  EraseInstFromFunction(*II);
2082  return BinaryOperator::CreateAdd(LHS, RHS);
2083  }
2084 
2085  // If the normal result of the add is dead, and the RHS is a constant,
2086  // we can transform this into a range comparison.
2087  // overflow = uadd a, -4 --> overflow = icmp ugt a, 3
2088  if (II->getIntrinsicID() == Intrinsic::uadd_with_overflow)
2089  if (ConstantInt *CI = dyn_cast<ConstantInt>(II->getArgOperand(1)))
2090  return new ICmpInst(ICmpInst::ICMP_UGT, II->getArgOperand(0),
2091  ConstantExpr::getNot(CI));
2092  break;
2093  case Intrinsic::usub_with_overflow:
2094  case Intrinsic::ssub_with_overflow:
2095  if (*EV.idx_begin() == 0) { // Normal result.
2096  Value *LHS = II->getArgOperand(0), *RHS = II->getArgOperand(1);
2097  ReplaceInstUsesWith(*II, UndefValue::get(II->getType()));
2098  EraseInstFromFunction(*II);
2099  return BinaryOperator::CreateSub(LHS, RHS);
2100  }
2101  break;
2102  case Intrinsic::umul_with_overflow:
2103  case Intrinsic::smul_with_overflow:
2104  if (*EV.idx_begin() == 0) { // Normal result.
2105  Value *LHS = II->getArgOperand(0), *RHS = II->getArgOperand(1);
2106  ReplaceInstUsesWith(*II, UndefValue::get(II->getType()));
2107  EraseInstFromFunction(*II);
2108  return BinaryOperator::CreateMul(LHS, RHS);
2109  }
2110  break;
2111  default:
2112  break;
2113  }
2114  }
2115  }
2116  if (LoadInst *L = dyn_cast<LoadInst>(Agg))
2117  // If the (non-volatile) load only has one use, we can rewrite this to a
2118  // load from a GEP. This reduces the size of the load.
2119  // FIXME: If a load is used only by extractvalue instructions then this
2120  // could be done regardless of having multiple uses.
2121  if (L->isSimple() && L->hasOneUse()) {
2122  // extractvalue has integer indices, getelementptr has Value*s. Convert.
2123  SmallVector<Value*, 4> Indices;
2124  // Prefix an i32 0 since we need the first element.
2125  Indices.push_back(Builder->getInt32(0));
2126  for (ExtractValueInst::idx_iterator I = EV.idx_begin(), E = EV.idx_end();
2127  I != E; ++I)
2128  Indices.push_back(Builder->getInt32(*I));
2129 
2130  // We need to insert these at the location of the old load, not at that of
2131  // the extractvalue.
2132  Builder->SetInsertPoint(L->getParent(), L);
2133  Value *GEP = Builder->CreateInBoundsGEP(L->getPointerOperand(), Indices);
2134  // Returning the load directly will cause the main loop to insert it in
2135  // the wrong spot, so use ReplaceInstUsesWith().
2136  return ReplaceInstUsesWith(EV, Builder->CreateLoad(GEP));
2137  }
2138  // We could simplify extracts from other values. Note that nested extracts may
2139  // already be simplified implicitly by the above: extract (extract (insert) )
2140  // will be translated into extract ( insert ( extract ) ) first and then just
2141  // the value inserted, if appropriate. Similarly for extracts from single-use
2142  // loads: extract (extract (load)) will be translated to extract (load (gep))
2143  // and if again single-use then via load (gep (gep)) to load (gep).
2144  // However, double extracts from e.g. function arguments or return values
2145  // aren't handled yet.
2146  return nullptr;
2147 }
LoadInst * CreateLoad(Value *Ptr, const char *Name)
Definition: IRBuilder.h:901
bool hasIndices() const
ArrayRef< unsigned > getIndices() const
Value * CreateExtractValue(Value *Agg, ArrayRef< unsigned > Idxs, const Twine &Name="")
Definition: IRBuilder.h:1416
ArrayRef< T > makeArrayRef(const T &OneElt)
Construct an ArrayRef from a single element.
Definition: ArrayRef.h:276
unsigned getNumIndices() const
void SetInsertPoint(BasicBlock *TheBB)
This specifies that created instructions should be appended to the end of the specified block...
Definition: IRBuilder.h:83
static ExtractValueInst * Create(Value *Agg, ArrayRef< unsigned > Idxs, const Twine &NameStr="", Instruction *InsertBefore=nullptr)
LLVM Constant Representation.
Definition: Constant.h:41
Instruction * ReplaceInstUsesWith(Instruction &I, Value *V)
Definition: InstCombine.h:282
const unsigned * idx_iterator
Represent an integer comparison operator.
Definition: Instructions.h:977
static Constant * getNot(Constant *C)
Definition: Constants.cpp:2096
BuilderTy * Builder
Definition: InstCombine.h:102
Value * CreateInBoundsGEP(Value *Ptr, ArrayRef< Value * > IdxList, const Twine &Name="")
Definition: IRBuilder.h:970
static UndefValue * get(Type *T)
Definition: Constants.cpp:1372
Class for constant integers.
Definition: Constants.h:51
ConstantInt * getInt32(uint32_t C)
Get a constant 32-bit value.
Definition: IRBuilder.h:276
idx_iterator idx_end() const
static InsertValueInst * Create(Value *Agg, Value *Val, ArrayRef< unsigned > Idxs, const Twine &NameStr="", Instruction *InsertBefore=nullptr)
#define I(x, y, z)
Definition: MD5.cpp:54
LLVM Value Representation.
Definition: Value.h:69
Instruction * EraseInstFromFunction(Instruction &I)
Definition: InstCombine.h:301
unsigned greater than
Definition: InstrTypes.h:695
idx_iterator idx_begin() const
Instruction * InstCombiner::visitFAdd ( BinaryOperator I)

Definition at line 1272 of file InstCombineAddSub.cpp.

1272  {
1273  bool Changed = SimplifyAssociativeOrCommutative(I);
1274  Value *LHS = I.getOperand(0), *RHS = I.getOperand(1);
1275 
1276  if (Value *V = SimplifyVectorOp(I))
1277  return ReplaceInstUsesWith(I, V);
1278 
1279  if (Value *V = SimplifyFAddInst(LHS, RHS, I.getFastMathFlags(), DL))
1280  return ReplaceInstUsesWith(I, V);
1281 
1282  if (isa<Constant>(RHS)) {
1283  if (isa<PHINode>(LHS))
1284  if (Instruction *NV = FoldOpIntoPhi(I))
1285  return NV;
1286 
1287  if (SelectInst *SI = dyn_cast<SelectInst>(LHS))
1288  if (Instruction *NV = FoldOpIntoSelect(I, SI))
1289  return NV;
1290  }
1291 
1292  // -A + B --> B - A
1293  // -A + -B --> -(A + B)
1294  if (Value *LHSV = dyn_castFNegVal(LHS)) {
1295  Instruction *RI = BinaryOperator::CreateFSub(RHS, LHSV);
1296  RI->copyFastMathFlags(&I);
1297  return RI;
1298  }
1299 
1300  // A + -B --> A - B
1301  if (!isa<Constant>(RHS))
1302  if (Value *V = dyn_castFNegVal(RHS)) {
1303  Instruction *RI = BinaryOperator::CreateFSub(LHS, V);
1304  RI->copyFastMathFlags(&I);
1305  return RI;
1306  }
1307 
1308  // Check for (fadd double (sitofp x), y), see if we can merge this into an
1309  // integer add followed by a promotion.
1310  if (SIToFPInst *LHSConv = dyn_cast<SIToFPInst>(LHS)) {
1311  // (fadd double (sitofp x), fpcst) --> (sitofp (add int x, intcst))
1312  // ... if the constant fits in the integer value. This is useful for things
1313  // like (double)(x & 1234) + 4.0 -> (double)((X & 1234)+4) which no longer
1314  // requires a constant pool load, and generally allows the add to be better
1315  // instcombined.
1316  if (ConstantFP *CFP = dyn_cast<ConstantFP>(RHS)) {
1317  Constant *CI =
1318  ConstantExpr::getFPToSI(CFP, LHSConv->getOperand(0)->getType());
1319  if (LHSConv->hasOneUse() &&
1320  ConstantExpr::getSIToFP(CI, I.getType()) == CFP &&
1321  WillNotOverflowSignedAdd(LHSConv->getOperand(0), CI)) {
1322  // Insert the new integer add.
1323  Value *NewAdd = Builder->CreateNSWAdd(LHSConv->getOperand(0),
1324  CI, "addconv");
1325  return new SIToFPInst(NewAdd, I.getType());
1326  }
1327  }
1328 
1329  // (fadd double (sitofp x), (sitofp y)) --> (sitofp (add int x, y))
1330  if (SIToFPInst *RHSConv = dyn_cast<SIToFPInst>(RHS)) {
1331  // Only do this if x/y have the same type, if at last one of them has a
1332  // single use (so we don't increase the number of int->fp conversions),
1333  // and if the integer add will not overflow.
1334  if (LHSConv->getOperand(0)->getType()==RHSConv->getOperand(0)->getType()&&
1335  (LHSConv->hasOneUse() || RHSConv->hasOneUse()) &&
1336  WillNotOverflowSignedAdd(LHSConv->getOperand(0),
1337  RHSConv->getOperand(0))) {
1338  // Insert the new integer add.
1339  Value *NewAdd = Builder->CreateNSWAdd(LHSConv->getOperand(0),
1340  RHSConv->getOperand(0),"addconv");
1341  return new SIToFPInst(NewAdd, I.getType());
1342  }
1343  }
1344  }
1345 
1346  // select C, 0, B + select C, A, 0 -> select C, A, B
1347  {
1348  Value *A1, *B1, *C1, *A2, *B2, *C2;
1349  if (match(LHS, m_Select(m_Value(C1), m_Value(A1), m_Value(B1))) &&
1350  match(RHS, m_Select(m_Value(C2), m_Value(A2), m_Value(B2)))) {
1351  if (C1 == C2) {
1352  Constant *Z1=nullptr, *Z2=nullptr;
1353  Value *A, *B, *C=C1;
1354  if (match(A1, m_AnyZero()) && match(B2, m_AnyZero())) {
1355  Z1 = dyn_cast<Constant>(A1); A = A2;
1356  Z2 = dyn_cast<Constant>(B2); B = B1;
1357  } else if (match(B1, m_AnyZero()) && match(A2, m_AnyZero())) {
1358  Z1 = dyn_cast<Constant>(B1); B = B2;
1359  Z2 = dyn_cast<Constant>(A2); A = A1;
1360  }
1361 
1362  if (Z1 && Z2 &&
1363  (I.hasNoSignedZeros() ||
1364  (Z1->isNegativeZeroValue() && Z2->isNegativeZeroValue()))) {
1365  return SelectInst::Create(C, A, B);
1366  }
1367  }
1368  }
1369  }
1370 
1371  if (I.hasUnsafeAlgebra()) {
1372  if (Value *V = FAddCombine(Builder).simplify(&I))
1373  return ReplaceInstUsesWith(I, V);
1374  }
1375 
1376  return Changed ? &I : nullptr;
1377 }
Instruction * FoldOpIntoPhi(Instruction &I)
class_match< Value > m_Value()
m_Value() - Match an arbitrary value and ignore it.
Definition: PatternMatch.h:70
static Constant * getSIToFP(Constant *C, Type *Ty)
Definition: Constants.cpp:1664
bool WillNotOverflowSignedAdd(Value *LHS, Value *RHS)
Value * SimplifyFAddInst(Value *LHS, Value *RHS, FastMathFlags FMF, const DataLayout *TD=nullptr, const TargetLibraryInfo *TLI=nullptr, const DominatorTree *DT=nullptr)
Value * SimplifyVectorOp(BinaryOperator &Inst)
Makes transformation of binary operation specific for vector types.
Instruction * FoldOpIntoSelect(Instruction &Op, SelectInst *SI)
FastMathFlags getFastMathFlags() const
Value * CreateNSWAdd(Value *LHS, Value *RHS, const Twine &Name="")
Definition: IRBuilder.h:640
bool isNegativeZeroValue() const
Definition: Constants.cpp:45
bool match(Val *V, const Pattern &P)
Definition: PatternMatch.h:42
static SelectInst * Create(Value *C, Value *S1, Value *S2, const Twine &NameStr="", Instruction *InsertBefore=nullptr)
bool hasUnsafeAlgebra() const
Determine whether the unsafe-algebra flag is set.
SelectClass_match< Cond, LHS, RHS > m_Select(const Cond &C, const LHS &L, const RHS &R)
Definition: PatternMatch.h:720
LLVM Constant Representation.
Definition: Constant.h:41
Instruction * ReplaceInstUsesWith(Instruction &I, Value *V)
Definition: InstCombine.h:282
Value * getOperand(unsigned i) const
Definition: User.h:90
BuilderTy * Builder
Definition: InstCombine.h:102
match_combine_or< match_zero, match_neg_zero > m_AnyZero()
Definition: PatternMatch.h:157
bool SimplifyAssociativeOrCommutative(BinaryOperator &I)
Type * getType() const
Definition: Value.h:215
void copyFastMathFlags(const Instruction *I)
Copy I's fast-math flags.
static Constant * getFPToSI(Constant *C, Type *Ty)
Definition: Constants.cpp:1686
LLVM_ATTRIBUTE_UNUSED_RESULT std::enable_if< !is_simple_type< Y >::value, typename cast_retty< X, const Y >::ret_type >::type dyn_cast(const Y &Val)
Definition: Casting.h:265
Value * dyn_castFNegVal(Value *V, bool NoSignedZero=false) const
bool hasNoSignedZeros() const
Determine whether the no-signed-zeros flag is set.
This class represents a cast from signed integer to floating point.
const DataLayout * DL
Definition: InstCombine.h:89
LLVM Value Representation.
Definition: Value.h:69
Instruction * InstCombiner::visitFCmpInst ( FCmpInst I)

Orders the operands of the compare so that they are listed from most complex to least complex. This puts constants before unary operators, before binary operators.

Definition at line 3492 of file InstCombineCompares.cpp.

3492  {
3493  bool Changed = false;
3494 
3495  /// Orders the operands of the compare so that they are listed from most
3496  /// complex to least complex. This puts constants before unary operators,
3497  /// before binary operators.
3498  if (getComplexity(I.getOperand(0)) < getComplexity(I.getOperand(1))) {
3499  I.swapOperands();
3500  Changed = true;
3501  }
3502 
3503  Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
3504 
3505  if (Value *V = SimplifyFCmpInst(I.getPredicate(), Op0, Op1, DL))
3506  return ReplaceInstUsesWith(I, V);
3507 
3508  // Simplify 'fcmp pred X, X'
3509  if (Op0 == Op1) {
3510  switch (I.getPredicate()) {
3511  default: llvm_unreachable("Unknown predicate!");
3512  case FCmpInst::FCMP_UNO: // True if unordered: isnan(X) | isnan(Y)
3513  case FCmpInst::FCMP_ULT: // True if unordered or less than
3514  case FCmpInst::FCMP_UGT: // True if unordered or greater than
3515  case FCmpInst::FCMP_UNE: // True if unordered or not equal
3516  // Canonicalize these to be 'fcmp uno %X, 0.0'.
3519  return &I;
3520 
3521  case FCmpInst::FCMP_ORD: // True if ordered (no nans)
3522  case FCmpInst::FCMP_OEQ: // True if ordered and equal
3523  case FCmpInst::FCMP_OGE: // True if ordered and greater than or equal
3524  case FCmpInst::FCMP_OLE: // True if ordered and less than or equal
3525  // Canonicalize these to be 'fcmp ord %X, 0.0'.
3528  return &I;
3529  }
3530  }
3531 
3532  // Handle fcmp with constant RHS
3533  if (Constant *RHSC = dyn_cast<Constant>(Op1)) {
3534  if (Instruction *LHSI = dyn_cast<Instruction>(Op0))
3535  switch (LHSI->getOpcode()) {
3536  case Instruction::FPExt: {
3537  // fcmp (fpext x), C -> fcmp x, (fptrunc C) if fptrunc is lossless
3538  FPExtInst *LHSExt = cast<FPExtInst>(LHSI);
3539  ConstantFP *RHSF = dyn_cast<ConstantFP>(RHSC);
3540  if (!RHSF)
3541  break;
3542 
3543  const fltSemantics *Sem;
3544  // FIXME: This shouldn't be here.
3545  if (LHSExt->getSrcTy()->isHalfTy())
3546  Sem = &APFloat::IEEEhalf;
3547  else if (LHSExt->getSrcTy()->isFloatTy())
3548  Sem = &APFloat::IEEEsingle;
3549  else if (LHSExt->getSrcTy()->isDoubleTy())
3550  Sem = &APFloat::IEEEdouble;
3551  else if (LHSExt->getSrcTy()->isFP128Ty())
3552  Sem = &APFloat::IEEEquad;
3553  else if (LHSExt->getSrcTy()->isX86_FP80Ty())
3555  else if (LHSExt->getSrcTy()->isPPC_FP128Ty())
3556  Sem = &APFloat::PPCDoubleDouble;
3557  else
3558  break;
3559 
3560  bool Lossy;
3561  APFloat F = RHSF->getValueAPF();
3562  F.convert(*Sem, APFloat::rmNearestTiesToEven, &Lossy);
3563 
3564  // Avoid lossy conversions and denormals. Zero is a special case
3565  // that's OK to convert.
3566  APFloat Fabs = F;
3567  Fabs.clearSign();
3568  if (!Lossy &&
3569  ((Fabs.compare(APFloat::getSmallestNormalized(*Sem)) !=
3570  APFloat::cmpLessThan) || Fabs.isZero()))
3571 
3572  return new FCmpInst(I.getPredicate(), LHSExt->getOperand(0),
3573  ConstantFP::get(RHSC->getContext(), F));
3574  break;
3575  }
3576  case Instruction::PHI:
3577  // Only fold fcmp into the PHI if the phi and fcmp are in the same
3578  // block. If in the same block, we're encouraging jump threading. If
3579  // not, we are just pessimizing the code by making an i1 phi.
3580  if (LHSI->getParent() == I.getParent())
3581  if (Instruction *NV = FoldOpIntoPhi(I))
3582  return NV;
3583  break;
3584  case Instruction::SIToFP:
3585  case Instruction::UIToFP:
3586  if (Instruction *NV = FoldFCmp_IntToFP_Cst(I, LHSI, RHSC))
3587  return NV;
3588  break;
3589  case Instruction::FSub: {
3590  // fcmp pred (fneg x), C -> fcmp swap(pred) x, -C
3591  Value *Op;
3592  if (match(LHSI, m_FNeg(m_Value(Op))))
3593  return new FCmpInst(I.getSwappedPredicate(), Op,
3594  ConstantExpr::getFNeg(RHSC));
3595  break;
3596  }
3597  case Instruction::Load:
3598  if (GetElementPtrInst *GEP =
3599  dyn_cast<GetElementPtrInst>(LHSI->getOperand(0))) {
3600  if (GlobalVariable *GV = dyn_cast<GlobalVariable>(GEP->getOperand(0)))
3601  if (GV->isConstant() && GV->hasDefinitiveInitializer() &&
3602  !cast<LoadInst>(LHSI)->isVolatile())
3603  if (Instruction *Res = FoldCmpLoadFromIndexedGlobal(GEP, GV, I))
3604  return Res;
3605  }
3606  break;
3607  case Instruction::Call: {
3608  CallInst *CI = cast<CallInst>(LHSI);
3610  // Various optimization for fabs compared with zero.
3611  if (RHSC->isNullValue() && CI->getCalledFunction() &&
3613  TLI->has(Func)) {
3614  if (Func == LibFunc::fabs || Func == LibFunc::fabsf ||
3615  Func == LibFunc::fabsl) {
3616  switch (I.getPredicate()) {
3617  default: break;
3618  // fabs(x) < 0 --> false
3619  case FCmpInst::FCMP_OLT:
3620  return ReplaceInstUsesWith(I, Builder->getFalse());
3621  // fabs(x) > 0 --> x != 0
3622  case FCmpInst::FCMP_OGT:
3623  return new FCmpInst(FCmpInst::FCMP_ONE, CI->getArgOperand(0),
3624  RHSC);
3625  // fabs(x) <= 0 --> x == 0
3626  case FCmpInst::FCMP_OLE:
3627  return new FCmpInst(FCmpInst::FCMP_OEQ, CI->getArgOperand(0),
3628  RHSC);
3629  // fabs(x) >= 0 --> !isnan(x)
3630  case FCmpInst::FCMP_OGE:
3631  return new FCmpInst(FCmpInst::FCMP_ORD, CI->getArgOperand(0),
3632  RHSC);
3633  // fabs(x) == 0 --> x == 0
3634  // fabs(x) != 0 --> x != 0
3635  case FCmpInst::FCMP_OEQ:
3636  case FCmpInst::FCMP_UEQ:
3637  case FCmpInst::FCMP_ONE:
3638  case FCmpInst::FCMP_UNE:
3639  return new FCmpInst(I.getPredicate(), CI->getArgOperand(0),
3640  RHSC);
3641  }
3642  }
3643  }
3644  }
3645  }
3646  }
3647 
3648  // fcmp pred (fneg x), (fneg y) -> fcmp swap(pred) x, y
3649  Value *X, *Y;
3650  if (match(Op0, m_FNeg(m_Value(X))) && match(Op1, m_FNeg(m_Value(Y))))
3651  return new FCmpInst(I.getSwappedPredicate(), X, Y);
3652 
3653  // fcmp (fpext x), (fpext y) -> fcmp x, y
3654  if (FPExtInst *LHSExt = dyn_cast<FPExtInst>(Op0))
3655  if (FPExtInst *RHSExt = dyn_cast<FPExtInst>(Op1))
3656  if (LHSExt->getSrcTy() == RHSExt->getSrcTy())
3657  return new FCmpInst(I.getPredicate(), LHSExt->getOperand(0),
3658  RHSExt->getOperand(0));
3659 
3660  return Changed ? &I : nullptr;
3661 }
Instruction * FoldOpIntoPhi(Instruction &I)
class_match< Value > m_Value()
m_Value() - Match an arbitrary value and ignore it.
Definition: PatternMatch.h:70
static const fltSemantics IEEEdouble
Definition: APFloat.h:133
0 1 0 0 True if ordered and less than
Definition: InstrTypes.h:678
static RegisterPass< NVPTXAllocaHoisting > X("alloca-hoisting","Hoisting alloca instructions in non-entry ""blocks to the entry block")
bool isDoubleTy() const
isDoubleTy - Return true if this is 'double', a 64-bit IEEE fp type.
Definition: Type.h:146
void clearSign()
Definition: APFloat.cpp:1616
1 1 1 0 True if unordered or not equal
Definition: InstrTypes.h:688
F(f)
Instruction * FoldFCmp_IntToFP_Cst(FCmpInst &I, Instruction *LHSI, Constant *RHSC)
fneg_match< LHS > m_FNeg(const LHS &L)
m_FNeg - Match a floating point negate.
Definition: PatternMatch.h:880
static Constant * getNullValue(Type *Ty)
Definition: Constants.cpp:133
StringRef getName() const
Definition: Value.cpp:168
1 0 0 1 True if unordered or equal
Definition: InstrTypes.h:683
long double fabsl(long double x);
bool match(Val *V, const Pattern &P)
Definition: PatternMatch.h:42
static const fltSemantics x87DoubleExtended
Definition: APFloat.h:136
TargetLibraryInfo * TLI
Definition: InstCombine.h:90
1 0 0 0 True if unordered: isnan(X) | isnan(Y)
Definition: InstrTypes.h:682
static unsigned getComplexity(Value *V)
Definition: InstCombine.h:47
bool getLibFunc(StringRef funcName, LibFunc::Func &F) const
bool has(LibFunc::Func F) const
#define llvm_unreachable(msg)
Definition: ErrorHandling.h:98
0 1 0 1 True if ordered and less than or equal
Definition: InstrTypes.h:679
static const fltSemantics IEEEquad
Definition: APFloat.h:134
bool isHalfTy() const
isHalfTy - Return true if this is 'half', a 16-bit IEEE fp type.
Definition: Type.h:140
Represents a floating point comparison operator.
bool isPPC_FP128Ty() const
isPPC_FP128Ty - Return true if this is powerpc long double.
Definition: Type.h:155
A self-contained host- and target-independent arbitrary-precision floating-point software implementat...
Definition: APFloat.h:122
static Constant * getFNeg(Constant *C)
Definition: Constants.cpp:2090
static APFloat getSmallestNormalized(const fltSemantics &Sem, bool Negative=false)
Definition: APFloat.cpp:3417
cmpResult compare(const APFloat &) const
Definition: APFloat.cpp:1879
LLVM Constant Representation.
Definition: Constant.h:41
Instruction * ReplaceInstUsesWith(Instruction &I, Value *V)
Definition: InstCombine.h:282
bool isFloatTy() const
isFloatTy - Return true if this is 'float', a 32-bit IEEE fp type.
Definition: Type.h:143
float fabsf(float x);
opStatus convert(const fltSemantics &, roundingMode, bool *)
Definition: APFloat.cpp:1958
Value * getOperand(unsigned i) const
Definition: User.h:90
0 1 1 1 True if ordered (no nans)
Definition: InstrTypes.h:681
Type * getSrcTy() const
Return the source type, as a convenience.
Definition: InstrTypes.h:629
Predicate getPredicate() const
Return the predicate for this instruction.
Definition: InstrTypes.h:735
BuilderTy * Builder
Definition: InstCombine.h:102
bool isFP128Ty() const
isFP128Ty - Return true if this is 'fp128'.
Definition: Type.h:152
static const fltSemantics IEEEhalf
Definition: APFloat.h:131
0 0 1 0 True if ordered and greater than
Definition: InstrTypes.h:676
static const fltSemantics PPCDoubleDouble
Definition: APFloat.h:135
1 1 0 0 True if unordered or less than
Definition: InstrTypes.h:686
Type * getType() const
Definition: Value.h:215
Predicate getSwappedPredicate() const
Return the predicate as if the operands were swapped.
Definition: InstrTypes.h:774
Function * getCalledFunction() const
double fabs(double x);
static Constant * get(Type *Ty, double V)
Definition: Constants.cpp:593
void setPredicate(Predicate P)
Set the predicate for this instruction to the specified value.
Definition: InstrTypes.h:740
void setOperand(unsigned i, Value *Val)
Definition: User.h:94
Value * getArgOperand(unsigned i) const
Value * SimplifyFCmpInst(unsigned Predicate, Value *LHS, Value *RHS, const DataLayout *TD=nullptr, const TargetLibraryInfo *TLI=nullptr, const DominatorTree *DT=nullptr)
LLVM_ATTRIBUTE_UNUSED_RESULT std::enable_if< !is_simple_type< Y >::value, typename cast_retty< X, const Y >::ret_type >::type dyn_cast(const Y &Val)
Definition: Casting.h:265
ConstantInt * getFalse()
Get the constant value for i1 false.
Definition: IRBuilder.h:261
static const fltSemantics IEEEsingle
Definition: APFloat.h:132
bool isX86_FP80Ty() const
isX86_FP80Ty - Return true if this is x86 long double.
Definition: Type.h:149
#define I(x, y, z)
Definition: MD5.cpp:54
0 1 1 0 True if ordered and operands are unequal
Definition: InstrTypes.h:680
1 0 1 0 True if unordered or greater than
Definition: InstrTypes.h:684
Instruction * FoldCmpLoadFromIndexedGlobal(GetElementPtrInst *GEP, GlobalVariable *GV, CmpInst &ICI, ConstantInt *AndCst=nullptr)
void swapOperands()
Swap operands and adjust predicate.
const DataLayout * DL
Definition: InstCombine.h:89
0 0 0 1 True if ordered and equal
Definition: InstrTypes.h:675
LLVM Value Representation.
Definition: Value.h:69
bool isZero() const
Returns true if and only if the float is plus or minus zero.
Definition: APFloat.h:377
static GCMetadataPrinterRegistry::Add< OcamlGCMetadataPrinter > Y("ocaml","ocaml 3.10-compatible collector")
This class represents an extension of floating point types.
0 0 1 1 True if ordered and greater than or equal
Definition: InstrTypes.h:677
const BasicBlock * getParent() const
Definition: Instruction.h:53
Instruction * InstCombiner::visitFDiv ( BinaryOperator I)

Definition at line 1061 of file InstCombineMulDivRem.cpp.

1061  {
1062  Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
1063 
1064  if (Value *V = SimplifyVectorOp(I))
1065  return ReplaceInstUsesWith(I, V);
1066 
1067  if (Value *V = SimplifyFDivInst(Op0, Op1, DL))
1068  return ReplaceInstUsesWith(I, V);
1069 
1070  if (isa<Constant>(Op0))
1071  if (SelectInst *SI = dyn_cast<SelectInst>(Op1))
1072  if (Instruction *R = FoldOpIntoSelect(I, SI))
1073  return R;
1074 
1075  bool AllowReassociate = I.hasUnsafeAlgebra();
1076  bool AllowReciprocal = I.hasAllowReciprocal();
1077 
1078  if (Constant *Op1C = dyn_cast<Constant>(Op1)) {
1079  if (SelectInst *SI = dyn_cast<SelectInst>(Op0))
1080  if (Instruction *R = FoldOpIntoSelect(I, SI))
1081  return R;
1082 
1083  if (AllowReassociate) {
1084  Constant *C1 = nullptr;
1085  Constant *C2 = Op1C;
1086  Value *X;
1087  Instruction *Res = nullptr;
1088 
1089  if (match(Op0, m_FMul(m_Value(X), m_Constant(C1)))) {
1090  // (X*C1)/C2 => X * (C1/C2)
1091  //
1092  Constant *C = ConstantExpr::getFDiv(C1, C2);
1093  if (isNormalFp(C))
1094  Res = BinaryOperator::CreateFMul(X, C);
1095  } else if (match(Op0, m_FDiv(m_Value(X), m_Constant(C1)))) {
1096  // (X/C1)/C2 => X /(C2*C1) [=> X * 1/(C2*C1) if reciprocal is allowed]
1097  //
1098  Constant *C = ConstantExpr::getFMul(C1, C2);
1099  if (isNormalFp(C)) {
1100  Res = CvtFDivConstToReciprocal(X, C, AllowReciprocal);
1101  if (!Res)
1102  Res = BinaryOperator::CreateFDiv(X, C);
1103  }
1104  }
1105 
1106  if (Res) {
1108  return Res;
1109  }
1110  }
1111 
1112  // X / C => X * 1/C
1113  if (Instruction *T = CvtFDivConstToReciprocal(Op0, Op1C, AllowReciprocal)) {
1114  T->copyFastMathFlags(&I);
1115  return T;
1116  }
1117 
1118  return nullptr;
1119  }
1120 
1121  if (AllowReassociate && isa<Constant>(Op0)) {
1122  Constant *C1 = cast<Constant>(Op0), *C2;
1123  Constant *Fold = nullptr;
1124  Value *X;
1125  bool CreateDiv = true;
1126 
1127  // C1 / (X*C2) => (C1/C2) / X
1128  if (match(Op1, m_FMul(m_Value(X), m_Constant(C2))))
1129  Fold = ConstantExpr::getFDiv(C1, C2);
1130  else if (match(Op1, m_FDiv(m_Value(X), m_Constant(C2)))) {
1131  // C1 / (X/C2) => (C1*C2) / X
1132  Fold = ConstantExpr::getFMul(C1, C2);
1133  } else if (match(Op1, m_FDiv(m_Constant(C2), m_Value(X)))) {
1134  // C1 / (C2/X) => (C1/C2) * X
1135  Fold = ConstantExpr::getFDiv(C1, C2);
1136  CreateDiv = false;
1137  }
1138 
1139  if (Fold && isNormalFp(Fold)) {
1140  Instruction *R = CreateDiv ? BinaryOperator::CreateFDiv(Fold, X)
1141  : BinaryOperator::CreateFMul(X, Fold);
1143  return R;
1144  }
1145  return nullptr;
1146  }
1147 
1148  if (AllowReassociate) {
1149  Value *X, *Y;
1150  Value *NewInst = nullptr;
1151  Instruction *SimpR = nullptr;
1152 
1153  if (Op0->hasOneUse() && match(Op0, m_FDiv(m_Value(X), m_Value(Y)))) {
1154  // (X/Y) / Z => X / (Y*Z)
1155  //
1156  if (!isa<Constant>(Y) || !isa<Constant>(Op1)) {
1157  NewInst = Builder->CreateFMul(Y, Op1);
1158  if (Instruction *RI = dyn_cast<Instruction>(NewInst)) {
1160  Flags &= cast<Instruction>(Op0)->getFastMathFlags();
1161  RI->setFastMathFlags(Flags);
1162  }
1163  SimpR = BinaryOperator::CreateFDiv(X, NewInst);
1164  }
1165  } else if (Op1->hasOneUse() && match(Op1, m_FDiv(m_Value(X), m_Value(Y)))) {
1166  // Z / (X/Y) => Z*Y / X
1167  //
1168  if (!isa<Constant>(Y) || !isa<Constant>(Op0)) {
1169  NewInst = Builder->CreateFMul(Op0, Y);
1170  if (Instruction *RI = dyn_cast<Instruction>(NewInst)) {
1172  Flags &= cast<Instruction>(Op1)->getFastMathFlags();
1173  RI->setFastMathFlags(Flags);
1174  }
1175  SimpR = BinaryOperator::CreateFDiv(NewInst, X);
1176  }
1177  }
1178 
1179  if (NewInst) {
1180  if (Instruction *T = dyn_cast<Instruction>(NewInst))
1181  T->setDebugLoc(I.getDebugLoc());
1182  SimpR->setFastMathFlags(I.getFastMathFlags());
1183  return SimpR;
1184  }
1185  }
1186 
1187  return nullptr;
1188 }
class_match< Value > m_Value()
m_Value() - Match an arbitrary value and ignore it.
Definition: PatternMatch.h:70
void setFastMathFlags(FastMathFlags FMF)
BinaryOp_match< LHS, RHS, Instruction::FDiv > m_FDiv(const LHS &L, const RHS &R)
Definition: PatternMatch.h:443
Value * SimplifyVectorOp(BinaryOperator &Inst)
Makes transformation of binary operation specific for vector types.
class_match< Constant > m_Constant()
Definition: PatternMatch.h:78
static RegisterPass< NVPTXAllocaHoisting > X("alloca-hoisting","Hoisting alloca instructions in non-entry ""blocks to the entry block")
Instruction * FoldOpIntoSelect(Instruction &Op, SelectInst *SI)
FastMathFlags getFastMathFlags() const
static Constant * getFMul(Constant *C1, Constant *C2)
Definition: Constants.cpp:2131
bool match(Val *V, const Pattern &P)
Definition: PatternMatch.h:42
bool hasAllowReciprocal() const
Determine whether the allow-reciprocal flag is set.
#define T
bool hasUnsafeAlgebra() const
Determine whether the unsafe-algebra flag is set.
static Constant * getFDiv(Constant *C1, Constant *C2)
Definition: Constants.cpp:2145
LLVM Constant Representation.
Definition: Constant.h:41
Instruction * ReplaceInstUsesWith(Instruction &I, Value *V)
Definition: InstCombine.h:282
const DebugLoc & getDebugLoc() const
getDebugLoc - Return the debug location for this node as a DebugLoc.
Definition: Instruction.h:196
Value * getOperand(unsigned i) const
Definition: User.h:90
static bool isNormalFp(Constant *C)
BuilderTy * Builder
Definition: InstCombine.h:102
static Instruction * CvtFDivConstToReciprocal(Value *Dividend, Constant *Divisor, bool AllowReciprocal)
BinaryOp_match< LHS, RHS, Instruction::FMul > m_FMul(const LHS &L, const RHS &R)
Definition: PatternMatch.h:425
bool hasOneUse() const
Definition: Value.h:284
Value * CreateFMul(Value *LHS, Value *RHS, const Twine &Name="", MDNode *FPMathTag=nullptr)
Definition: IRBuilder.h:690
const DataLayout * DL
Definition: InstCombine.h:89
Value * SimplifyFDivInst(Value *LHS, Value *RHS, const DataLayout *TD=nullptr, const TargetLibraryInfo *TLI=nullptr, const DominatorTree *DT=nullptr)
LLVM Value Representation.
Definition: Value.h:69
Convenience struct for specifying and reasoning about fast-math flags.
Definition: Operator.h:170
static GCMetadataPrinterRegistry::Add< OcamlGCMetadataPrinter > Y("ocaml","ocaml 3.10-compatible collector")
Instruction * llvm::InstVisitor< InstCombiner , Instruction * >::visitFenceInst ( FenceInst I)
inlineinherited

Definition at line 179 of file InstVisitor.h.

#define DELEGATE(CLASS_TO_VISIT)
Definition: InstVisitor.h:30
Instruction * InstCombiner::visitFMul ( BinaryOperator I)

Definition at line 433 of file InstCombineMulDivRem.cpp.

433  {
434  bool Changed = SimplifyAssociativeOrCommutative(I);
435  Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
436 
437  if (Value *V = SimplifyVectorOp(I))
438  return ReplaceInstUsesWith(I, V);
439 
440  if (isa<Constant>(Op0))
441  std::swap(Op0, Op1);
442 
443  if (Value *V = SimplifyFMulInst(Op0, Op1, I.getFastMathFlags(), DL))
444  return ReplaceInstUsesWith(I, V);
445 
446  bool AllowReassociate = I.hasUnsafeAlgebra();
447 
448  // Simplify mul instructions with a constant RHS.
449  if (isa<Constant>(Op1)) {
450  // Try to fold constant mul into select arguments.
451  if (SelectInst *SI = dyn_cast<SelectInst>(Op0))
452  if (Instruction *R = FoldOpIntoSelect(I, SI))
453  return R;
454 
455  if (isa<PHINode>(Op0))
456  if (Instruction *NV = FoldOpIntoPhi(I))
457  return NV;
458 
459  // (fmul X, -1.0) --> (fsub -0.0, X)
460  if (match(Op1, m_SpecificFP(-1.0))) {
461  Constant *NegZero = ConstantFP::getNegativeZero(Op1->getType());
462  Instruction *RI = BinaryOperator::CreateFSub(NegZero, Op0);
463  RI->copyFastMathFlags(&I);
464  return RI;
465  }
466 
467  Constant *C = cast<Constant>(Op1);
468  if (AllowReassociate && isFiniteNonZeroFp(C)) {
469  // Let MDC denote an expression in one of these forms:
470  // X * C, C/X, X/C, where C is a constant.
471  //
472  // Try to simplify "MDC * Constant"
473  if (isFMulOrFDivWithConstant(Op0))
474  if (Value *V = foldFMulConst(cast<Instruction>(Op0), C, &I))
475  return ReplaceInstUsesWith(I, V);
476 
477  // (MDC +/- C1) * C => (MDC * C) +/- (C1 * C)
478  Instruction *FAddSub = dyn_cast<Instruction>(Op0);
479  if (FAddSub &&
480  (FAddSub->getOpcode() == Instruction::FAdd ||
481  FAddSub->getOpcode() == Instruction::FSub)) {
482  Value *Opnd0 = FAddSub->getOperand(0);
483  Value *Opnd1 = FAddSub->getOperand(1);
484  Constant *C0 = dyn_cast<Constant>(Opnd0);
485  Constant *C1 = dyn_cast<Constant>(Opnd1);
486  bool Swap = false;
487  if (C0) {
488  std::swap(C0, C1);
489  std::swap(Opnd0, Opnd1);
490  Swap = true;
491  }
492 
493  if (C1 && isFiniteNonZeroFp(C1) && isFMulOrFDivWithConstant(Opnd0)) {
494  Value *M1 = ConstantExpr::getFMul(C1, C);
495  Value *M0 = isNormalFp(cast<Constant>(M1)) ?
496  foldFMulConst(cast<Instruction>(Opnd0), C, &I) :
497  nullptr;
498  if (M0 && M1) {
499  if (Swap && FAddSub->getOpcode() == Instruction::FSub)
500  std::swap(M0, M1);
501 
502  Instruction *RI = (FAddSub->getOpcode() == Instruction::FAdd)
503  ? BinaryOperator::CreateFAdd(M0, M1)
504  : BinaryOperator::CreateFSub(M0, M1);
505  RI->copyFastMathFlags(&I);
506  return RI;
507  }
508  }
509  }
510  }
511  }
512 
513 
514  // Under unsafe algebra do:
515  // X * log2(0.5*Y) = X*log2(Y) - X
516  if (I.hasUnsafeAlgebra()) {
517  Value *OpX = nullptr;
518  Value *OpY = nullptr;
519  IntrinsicInst *Log2;
520  detectLog2OfHalf(Op0, OpY, Log2);
521  if (OpY) {
522  OpX = Op1;
523  } else {
524  detectLog2OfHalf(Op1, OpY, Log2);
525  if (OpY) {
526  OpX = Op0;
527  }
528  }
529  // if pattern detected emit alternate sequence
530  if (OpX && OpY) {
533  Log2->setArgOperand(0, OpY);
534  Value *FMulVal = Builder->CreateFMul(OpX, Log2);
535  Value *FSub = Builder->CreateFSub(FMulVal, OpX);
536  FSub->takeName(&I);
537  return ReplaceInstUsesWith(I, FSub);
538  }
539  }
540 
541  // Handle symmetric situation in a 2-iteration loop
542  Value *Opnd0 = Op0;
543  Value *Opnd1 = Op1;
544  for (int i = 0; i < 2; i++) {
545  bool IgnoreZeroSign = I.hasNoSignedZeros();
546  if (BinaryOperator::isFNeg(Opnd0, IgnoreZeroSign)) {
549 
550  Value *N0 = dyn_castFNegVal(Opnd0, IgnoreZeroSign);
551  Value *N1 = dyn_castFNegVal(Opnd1, IgnoreZeroSign);
552 
553  // -X * -Y => X*Y
554  if (N1) {
555  Value *FMul = Builder->CreateFMul(N0, N1);
556  FMul->takeName(&I);
557  return ReplaceInstUsesWith(I, FMul);
558  }
559 
560  if (Opnd0->hasOneUse()) {
561  // -X * Y => -(X*Y) (Promote negation as high as possible)
562  Value *T = Builder->CreateFMul(N0, Opnd1);
563  Value *Neg = Builder->CreateFNeg(T);
564  Neg->takeName(&I);
565  return ReplaceInstUsesWith(I, Neg);
566  }
567  }
568 
569  // (X*Y) * X => (X*X) * Y where Y != X
570  // The purpose is two-fold:
571  // 1) to form a power expression (of X).
572  // 2) potentially shorten the critical path: After transformation, the
573  // latency of the instruction Y is amortized by the expression of X*X,
574  // and therefore Y is in a "less critical" position compared to what it
575  // was before the transformation.
576  //
577  if (AllowReassociate) {
578  Value *Opnd0_0, *Opnd0_1;
579  if (Opnd0->hasOneUse() &&
580  match(Opnd0, m_FMul(m_Value(Opnd0_0), m_Value(Opnd0_1)))) {
581  Value *Y = nullptr;
582  if (Opnd0_0 == Opnd1 && Opnd0_1 != Opnd1)
583  Y = Opnd0_1;
584  else if (Opnd0_1 == Opnd1 && Opnd0_0 != Opnd1)
585  Y = Opnd0_0;
586 
587  if (Y) {
590  Value *T = Builder->CreateFMul(Opnd1, Opnd1);
591 
592  Value *R = Builder->CreateFMul(T, Y);
593  R->takeName(&I);
594  return ReplaceInstUsesWith(I, R);
595  }
596  }
597  }
598 
599  // B * (uitofp i1 C) -> select C, B, 0
600  if (I.hasNoNaNs() && I.hasNoInfs() && I.hasNoSignedZeros()) {
601  Value *LHS = Op0, *RHS = Op1;
602  Value *B, *C;
603  if (!match(RHS, m_UIToFP(m_Value(C))))
604  std::swap(LHS, RHS);
605 
606  if (match(RHS, m_UIToFP(m_Value(C))) &&
607  C->getType()->getScalarType()->isIntegerTy(1)) {
608  B = LHS;
610  return SelectInst::Create(C, B, Zero);
611  }
612  }
613 
614  // A * (1 - uitofp i1 C) -> select C, 0, A
615  if (I.hasNoNaNs() && I.hasNoInfs() && I.hasNoSignedZeros()) {
616  Value *LHS = Op0, *RHS = Op1;
617  Value *A, *C;
618  if (!match(RHS, m_FSub(m_FPOne(), m_UIToFP(m_Value(C)))))
619  std::swap(LHS, RHS);
620 
621  if (match(RHS, m_FSub(m_FPOne(), m_UIToFP(m_Value(C)))) &&
622  C->getType()->getScalarType()->isIntegerTy(1)) {
623  A = LHS;
625  return SelectInst::Create(C, Zero, A);
626  }
627  }
628 
629  if (!isa<Constant>(Op1))
630  std::swap(Opnd0, Opnd1);
631  else
632  break;
633  }
634 
635  return Changed ? &I : nullptr;
636 }
Instruction * FoldOpIntoPhi(Instruction &I)
class_match< Value > m_Value()
m_Value() - Match an arbitrary value and ignore it.
Definition: PatternMatch.h:70
Value * SimplifyVectorOp(BinaryOperator &Inst)
Makes transformation of binary operation specific for vector types.
Instruction * FoldOpIntoSelect(Instruction &Op, SelectInst *SI)
FastMathFlags getFastMathFlags() const
BinaryOp_match< LHS, RHS, Instruction::FSub > m_FSub(const LHS &L, const RHS &R)
Definition: PatternMatch.h:413
static Constant * getFMul(Constant *C1, Constant *C2)
Definition: Constants.cpp:2131
specific_fpval m_SpecificFP(double V)
Definition: PatternMatch.h:345
bool match(Val *V, const Pattern &P)
Definition: PatternMatch.h:42
bool hasNoNaNs() const
Determine whether the no-NaNs flag is set.
static Constant * getNegativeZero(Type *Ty)
Definition: Constants.cpp:623
static bool isFiniteNonZeroFp(Constant *C)
static SelectInst * Create(Value *C, Value *S1, Value *S2, const Twine &NameStr="", Instruction *InsertBefore=nullptr)
void SetFastMathFlags(FastMathFlags NewFMF)
Set the fast-math flags to be used with generated fp-math operators.
Definition: IRBuilder.h:189
#define T
bool hasUnsafeAlgebra() const
Determine whether the unsafe-algebra flag is set.
void takeName(Value *V)
Definition: Value.cpp:242
LLVM Constant Representation.
Definition: Constant.h:41
Instruction * ReplaceInstUsesWith(Instruction &I, Value *V)
Definition: InstCombine.h:282
Value * foldFMulConst(Instruction *FMulOrDiv, Constant *C, Instruction *InsertBefore)
Value * getOperand(unsigned i) const
Definition: User.h:90
static bool isNormalFp(Constant *C)
BuilderTy * Builder
Definition: InstCombine.h:102
static bool isFMulOrFDivWithConstant(Value *V)
bool SimplifyAssociativeOrCommutative(BinaryOperator &I)
Type * getType() const
Definition: Value.h:215
CastClass_match< OpTy, Instruction::UIToFP > m_UIToFP(const OpTy &Op)
m_UIToFP
Definition: PatternMatch.h:789
void copyFastMathFlags(const Instruction *I)
Copy I's fast-math flags.
BinaryOp_match< LHS, RHS, Instruction::FMul > m_FMul(const LHS &L, const RHS &R)
Definition: PatternMatch.h:425
void swap(llvm::BitVector &LHS, llvm::BitVector &RHS)
Implement std::swap in terms of BitVector swap.
Definition: BitVector.h:590
bool isIntegerTy() const
Definition: Type.h:193
LLVM_ATTRIBUTE_UNUSED_RESULT std::enable_if< !is_simple_type< Y >::value, typename cast_retty< X, const Y >::ret_type >::type dyn_cast(const Y &Val)
Definition: Casting.h:265
Value * dyn_castFNegVal(Value *V, bool NoSignedZero=false) const
const Type * getScalarType() const LLVM_READONLY
Definition: Type.cpp:51
specific_fpval m_FPOne()
Match a float 1.0 or vector with all elements equal to 1.0.
Definition: PatternMatch.h:348
static bool isFNeg(const Value *V, bool IgnoreZeroSign=false)
bool hasOneUse() const
Definition: Value.h:284
bool hasNoInfs() const
Determine whether the no-infs flag is set.
void setArgOperand(unsigned i, Value *v)
bool hasNoSignedZeros() const
Determine whether the no-signed-zeros flag is set.
Value * CreateFMul(Value *LHS, Value *RHS, const Twine &Name="", MDNode *FPMathTag=nullptr)
Definition: IRBuilder.h:690
const DataLayout * DL
Definition: InstCombine.h:89
LLVM Value Representation.
Definition: Value.h:69
unsigned getOpcode() const
getOpcode() returns a member of one of the enums like Instruction::Add.
Definition: Instruction.h:86
static GCMetadataPrinterRegistry::Add< OcamlGCMetadataPrinter > Y("ocaml","ocaml 3.10-compatible collector")
static void detectLog2OfHalf(Value *&Op, Value *&Y, IntrinsicInst *&Log2)
Value * CreateFNeg(Value *V, const Twine &Name="", MDNode *FPMathTag=nullptr)
Definition: IRBuilder.h:878
Value * CreateFSub(Value *LHS, Value *RHS, const Twine &Name="", MDNode *FPMathTag=nullptr)
Definition: IRBuilder.h:668
Value * SimplifyFMulInst(Value *LHS, Value *RHS, FastMathFlags FMF, const DataLayout *TD=nullptr, const TargetLibraryInfo *TLI=nullptr, const DominatorTree *DT=nullptr)
Instruction * InstCombiner::visitFPExt ( CastInst CI)

Definition at line 1354 of file InstCombineCasts.cpp.

1354  {
1355  return commonCastTransforms(CI);
1356 }
Instruction * commonCastTransforms(CastInst &CI)
Implement the transforms common to all CastInst visitors.
Instruction * llvm::InstVisitor< InstCombiner , Instruction * >::visitFPExtInst ( FPExtInst I)
inlineinherited

Definition at line 186 of file InstVisitor.h.

186 { DELEGATE(CastInst);}
#define DELEGATE(CLASS_TO_VISIT)
Definition: InstVisitor.h:30
Instruction * InstCombiner::visitFPToSI ( FPToSIInst FI)

Definition at line 1378 of file InstCombineCasts.cpp.

1378  {
1379  Instruction *OpI = dyn_cast<Instruction>(FI.getOperand(0));
1380  if (!OpI)
1381  return commonCastTransforms(FI);
1382 
1383  // fptosi(sitofp(X)) --> X
1384  // fptosi(uitofp(X)) --> X
1385  // This is safe if the intermediate type has enough bits in its mantissa to
1386  // accurately represent all values of X. For example, do not do this with
1387  // i64->float->i64. This is also safe for sitofp case, because any negative
1388  // 'X' value would cause an undefined result for the fptoui.
1389  if ((isa<UIToFPInst>(OpI) || isa<SIToFPInst>(OpI)) &&
1390  OpI->getOperand(0)->getType() == FI.getType() &&
1391  (int)FI.getType()->getScalarSizeInBits() <=
1392  OpI->getType()->getFPMantissaWidth())
1393  return ReplaceInstUsesWith(FI, OpI->getOperand(0));
1394 
1395  return commonCastTransforms(FI);
1396 }
Instruction * commonCastTransforms(CastInst &CI)
Implement the transforms common to all CastInst visitors.
int getFPMantissaWidth() const
Definition: Type.cpp:142
Instruction * ReplaceInstUsesWith(Instruction &I, Value *V)
Definition: InstCombine.h:282
Value * getOperand(unsigned i) const
Definition: User.h:90
unsigned getScalarSizeInBits() const LLVM_READONLY
Definition: Type.cpp:135
Type * getType() const
Definition: Value.h:215
LLVM_ATTRIBUTE_UNUSED_RESULT std::enable_if< !is_simple_type< Y >::value, typename cast_retty< X, const Y >::ret_type >::type dyn_cast(const Y &Val)
Definition: Casting.h:265
Instruction * llvm::InstVisitor< InstCombiner , Instruction * >::visitFPToSIInst ( FPToSIInst I)
inlineinherited

Definition at line 188 of file InstVisitor.h.

188 { DELEGATE(CastInst);}
#define DELEGATE(CLASS_TO_VISIT)
Definition: InstVisitor.h:30
Instruction * InstCombiner::visitFPToUI ( FPToUIInst FI)

Definition at line 1358 of file InstCombineCasts.cpp.

1358  {
1359  Instruction *OpI = dyn_cast<Instruction>(FI.getOperand(0));
1360  if (!OpI)
1361  return commonCastTransforms(FI);
1362 
1363  // fptoui(uitofp(X)) --> X
1364  // fptoui(sitofp(X)) --> X
1365  // This is safe if the intermediate type has enough bits in its mantissa to
1366  // accurately represent all values of X. For example, do not do this with
1367  // i64->float->i64. This is also safe for sitofp case, because any negative
1368  // 'X' value would cause an undefined result for the fptoui.
1369  if ((isa<UIToFPInst>(OpI) || isa<SIToFPInst>(OpI)) &&
1370  OpI->getOperand(0)->getType() == FI.getType() &&
1371  (int)FI.getType()->getScalarSizeInBits() < /*extra bit for sign */
1372  OpI->getType()->getFPMantissaWidth())
1373  return ReplaceInstUsesWith(FI, OpI->getOperand(0));
1374 
1375  return commonCastTransforms(FI);
1376 }
Instruction * commonCastTransforms(CastInst &CI)
Implement the transforms common to all CastInst visitors.
int getFPMantissaWidth() const
Definition: Type.cpp:142
Instruction * ReplaceInstUsesWith(Instruction &I, Value *V)
Definition: InstCombine.h:282
Value * getOperand(unsigned i) const
Definition: User.h:90
unsigned getScalarSizeInBits() const LLVM_READONLY
Definition: Type.cpp:135
Type * getType() const
Definition: Value.h:215
LLVM_ATTRIBUTE_UNUSED_RESULT std::enable_if< !is_simple_type< Y >::value, typename cast_retty< X, const Y >::ret_type >::type dyn_cast(const Y &Val)
Definition: Casting.h:265
Instruction * llvm::InstVisitor< InstCombiner , Instruction * >::visitFPToUIInst ( FPToUIInst I)
inlineinherited

Definition at line 187 of file InstVisitor.h.

187 { DELEGATE(CastInst);}
#define DELEGATE(CLASS_TO_VISIT)
Definition: InstVisitor.h:30
Instruction * InstCombiner::visitFPTrunc ( FPTruncInst CI)

Definition at line 1171 of file InstCombineCasts.cpp.

1171  {
1172  if (Instruction *I = commonCastTransforms(CI))
1173  return I;
1174  // If we have fptrunc(OpI (fpextend x), (fpextend y)), we would like to
1175  // simpilify this expression to avoid one or more of the trunc/extend
1176  // operations if we can do so without changing the numerical results.
1177  //
1178  // The exact manner in which the widths of the operands interact to limit
1179  // what we can and cannot do safely varies from operation to operation, and
1180  // is explained below in the various case statements.
1182  if (OpI && OpI->hasOneUse()) {
1183  Value *LHSOrig = LookThroughFPExtensions(OpI->getOperand(0));
1184  Value *RHSOrig = LookThroughFPExtensions(OpI->getOperand(1));
1185  unsigned OpWidth = OpI->getType()->getFPMantissaWidth();
1186  unsigned LHSWidth = LHSOrig->getType()->getFPMantissaWidth();
1187  unsigned RHSWidth = RHSOrig->getType()->getFPMantissaWidth();
1188  unsigned SrcWidth = std::max(LHSWidth, RHSWidth);
1189  unsigned DstWidth = CI.getType()->getFPMantissaWidth();
1190  switch (OpI->getOpcode()) {
1191  default: break;
1192  case Instruction::FAdd:
1193  case Instruction::FSub:
1194  // For addition and subtraction, the infinitely precise result can
1195  // essentially be arbitrarily wide; proving that double rounding
1196  // will not occur because the result of OpI is exact (as we will for
1197  // FMul, for example) is hopeless. However, we *can* nonetheless
1198  // frequently know that double rounding cannot occur (or that it is
1199  // innocuous) by taking advantage of the specific structure of
1200  // infinitely-precise results that admit double rounding.
1201  //
1202  // Specifically, if OpWidth >= 2*DstWdith+1 and DstWidth is sufficient
1203  // to represent both sources, we can guarantee that the double
1204  // rounding is innocuous (See p50 of Figueroa's 2000 PhD thesis,
1205  // "A Rigorous Framework for Fully Supporting the IEEE Standard ..."
1206  // for proof of this fact).
1207  //
1208  // Note: Figueroa does not consider the case where DstFormat !=
1209  // SrcFormat. It's possible (likely even!) that this analysis
1210  // could be tightened for those cases, but they are rare (the main
1211  // case of interest here is (float)((double)float + float)).
1212  if (OpWidth >= 2*DstWidth+1 && DstWidth >= SrcWidth) {
1213  if (LHSOrig->getType() != CI.getType())
1214  LHSOrig = Builder->CreateFPExt(LHSOrig, CI.getType());
1215  if (RHSOrig->getType() != CI.getType())
1216  RHSOrig = Builder->CreateFPExt(RHSOrig, CI.getType());
1217  Instruction *RI =
1218  BinaryOperator::Create(OpI->getOpcode(), LHSOrig, RHSOrig);
1219  RI->copyFastMathFlags(OpI);
1220  return RI;
1221  }
1222  break;
1223  case Instruction::FMul:
1224  // For multiplication, the infinitely precise result has at most
1225  // LHSWidth + RHSWidth significant bits; if OpWidth is sufficient
1226  // that such a value can be exactly represented, then no double
1227  // rounding can possibly occur; we can safely perform the operation
1228  // in the destination format if it can represent both sources.
1229  if (OpWidth >= LHSWidth + RHSWidth && DstWidth >= SrcWidth) {
1230  if (LHSOrig->getType() != CI.getType())
1231  LHSOrig = Builder->CreateFPExt(LHSOrig, CI.getType());
1232  if (RHSOrig->getType() != CI.getType())
1233  RHSOrig = Builder->CreateFPExt(RHSOrig, CI.getType());
1234  Instruction *RI =
1235  BinaryOperator::CreateFMul(LHSOrig, RHSOrig);
1236  RI->copyFastMathFlags(OpI);
1237  return RI;
1238  }
1239  break;
1240  case Instruction::FDiv:
1241  // For division, we use again use the bound from Figueroa's
1242  // dissertation. I am entirely certain that this bound can be
1243  // tightened in the unbalanced operand case by an analysis based on
1244  // the diophantine rational approximation bound, but the well-known
1245  // condition used here is a good conservative first pass.
1246  // TODO: Tighten bound via rigorous analysis of the unbalanced case.
1247  if (OpWidth >= 2*DstWidth && DstWidth >= SrcWidth) {
1248  if (LHSOrig->getType() != CI.getType())
1249  LHSOrig = Builder->CreateFPExt(LHSOrig, CI.getType());
1250  if (RHSOrig->getType() != CI.getType())
1251  RHSOrig = Builder->CreateFPExt(RHSOrig, CI.getType());
1252  Instruction *RI =
1253  BinaryOperator::CreateFDiv(LHSOrig, RHSOrig);
1254  RI->copyFastMathFlags(OpI);
1255  return RI;
1256  }
1257  break;
1258  case Instruction::FRem:
1259  // Remainder is straightforward. Remainder is always exact, so the
1260  // type of OpI doesn't enter into things at all. We simply evaluate
1261  // in whichever source type is larger, then convert to the
1262  // destination type.
1263  if (LHSWidth < SrcWidth)
1264  LHSOrig = Builder->CreateFPExt(LHSOrig, RHSOrig->getType());
1265  else if (RHSWidth <= SrcWidth)
1266  RHSOrig = Builder->CreateFPExt(RHSOrig, LHSOrig->getType());
1267  Value *ExactResult = Builder->CreateFRem(LHSOrig, RHSOrig);
1268  if (Instruction *RI = dyn_cast<Instruction>(ExactResult))
1269  RI->copyFastMathFlags(OpI);
1270  return CastInst::CreateFPCast(ExactResult, CI.getType());
1271  }
1272 
1273  // (fptrunc (fneg x)) -> (fneg (fptrunc x))
1274  if (BinaryOperator::isFNeg(OpI)) {
1275  Value *InnerTrunc = Builder->CreateFPTrunc(OpI->getOperand(1),
1276  CI.getType());
1277  Instruction *RI = BinaryOperator::CreateFNeg(InnerTrunc);
1278  RI->copyFastMathFlags(OpI);
1279  return RI;
1280  }
1281  }
1282 
1283  // (fptrunc (select cond, R1, Cst)) -->
1284  // (select cond, (fptrunc R1), (fptrunc Cst))
1285  SelectInst *SI = dyn_cast<SelectInst>(CI.getOperand(0));
1286  if (SI &&
1287  (isa<ConstantFP>(SI->getOperand(1)) ||
1288  isa<ConstantFP>(SI->getOperand(2)))) {
1289  Value *LHSTrunc = Builder->CreateFPTrunc(SI->getOperand(1),
1290  CI.getType());
1291  Value *RHSTrunc = Builder->CreateFPTrunc(SI->getOperand(2),
1292  CI.getType());
1293  return SelectInst::Create(SI->getOperand(0), LHSTrunc, RHSTrunc);
1294  }
1295 
1297  if (II) {
1298  switch (II->getIntrinsicID()) {
1299  default: break;
1300  case Intrinsic::fabs: {
1301  // (fptrunc (fabs x)) -> (fabs (fptrunc x))
1302  Value *InnerTrunc = Builder->CreateFPTrunc(II->getArgOperand(0),
1303  CI.getType());
1304  Type *IntrinsicType[] = { CI.getType() };
1305  Function *Overload =
1307  II->getIntrinsicID(), IntrinsicType);
1308 
1309  Value *Args[] = { InnerTrunc };
1310  return CallInst::Create(Overload, Args, II->getName());
1311  }
1312  }
1313  }
1314 
1315  // Fold (fptrunc (sqrt (fpext x))) -> (sqrtf x)
1316  // Note that we restrict this transformation based on
1317  // TLI->has(LibFunc::sqrtf), even for the sqrt intrinsic, because
1318  // TLI->has(LibFunc::sqrtf) is sufficient to guarantee that the
1319  // single-precision intrinsic can be expanded in the backend.
1321  if (Call && Call->getCalledFunction() && TLI->has(LibFunc::sqrtf) &&
1322  (Call->getCalledFunction()->getName() == TLI->getName(LibFunc::sqrt) ||
1324  Call->getNumArgOperands() == 1 &&
1325  Call->hasOneUse()) {
1326  CastInst *Arg = dyn_cast<CastInst>(Call->getArgOperand(0));
1327  if (Arg && Arg->getOpcode() == Instruction::FPExt &&
1328  CI.getType()->isFloatTy() &&
1329  Call->getType()->isDoubleTy() &&
1330  Arg->getType()->isDoubleTy() &&
1331  Arg->getOperand(0)->getType()->isFloatTy()) {
1332  Function *Callee = Call->getCalledFunction();
1333  Module *M = CI.getParent()->getParent()->getParent();
1334  Constant *SqrtfFunc = (Callee->getIntrinsicID() == Intrinsic::sqrt) ?
1336  M->getOrInsertFunction("sqrtf", Callee->getAttributes(),
1338  NULL);
1339  CallInst *ret = CallInst::Create(SqrtfFunc, Arg->getOperand(0),
1340  "sqrtfcall");
1341  ret->setAttributes(Callee->getAttributes());
1342 
1343 
1344  // Remove the old Call. With -fmath-errno, it won't get marked readnone.
1345  ReplaceInstUsesWith(*Call, UndefValue::get(Call->getType()));
1346  EraseInstFromFunction(*Call);
1347  return ret;
1348  }
1349  }
1350 
1351  return nullptr;
1352 }
Instruction::CastOps getOpcode() const
Return the opcode of this CastInst.
Definition: InstrTypes.h:624
The main container class for the LLVM Intermediate Representation.
Definition: Module.h:115
Intrinsic::ID getIntrinsicID() const
Definition: IntrinsicInst.h:43
Instruction * commonCastTransforms(CastInst &CI)
Implement the transforms common to all CastInst visitors.
bool isDoubleTy() const
isDoubleTy - Return true if this is 'double', a 64-bit IEEE fp type.
Definition: Type.h:146
int getFPMantissaWidth() const
Definition: Type.cpp:142
const Function * getParent() const
Return the enclosing method, or null if none.
Definition: BasicBlock.h:118
static Value * LookThroughFPExtensions(Value *V)
float sqrtf(float x);
StringRef getName() const
Definition: Value.cpp:168
TargetLibraryInfo * TLI
Definition: InstCombine.h:90
static CallInst * Create(Value *Func, ArrayRef< Value * > Args, const Twine &NameStr="", Instruction *InsertBefore=nullptr)
Base class of casting instructions.
Definition: InstrTypes.h:387
bool has(LibFunc::Func F) const
unsigned getNumArgOperands() const
static SelectInst * Create(Value *C, Value *S1, Value *S2, const Twine &NameStr="", Instruction *InsertBefore=nullptr)
double sqrt(double x);
Function * getDeclaration(Module *M, ID id, ArrayRef< Type * > Tys=None)
Definition: Function.cpp:737
Constant * getOrInsertFunction(StringRef Name, FunctionType *T, AttributeSet AttributeList)
Definition: Module.cpp:97
Value * CreateFRem(Value *LHS, Value *RHS, const Twine &Name="", MDNode *FPMathTag=nullptr)
Definition: IRBuilder.h:742
unsigned getIntrinsicID() const LLVM_READONLY
Definition: Function.cpp:406
LLVM Constant Representation.
Definition: Constant.h:41
Instruction * ReplaceInstUsesWith(Instruction &I, Value *V)
Definition: InstCombine.h:282
bool isFloatTy() const
isFloatTy - Return true if this is 'float', a 32-bit IEEE fp type.
Definition: Type.h:143
Value * getOperand(unsigned i) const
Definition: User.h:90
BuilderTy * Builder
Definition: InstCombine.h:102
static UndefValue * get(Type *T)
Definition: Constants.cpp:1372
BinaryOps getOpcode() const
Definition: InstrTypes.h:326
static BinaryOperator * CreateFNeg(Value *Op, const Twine &Name="", Instruction *InsertBefore=nullptr)
const char * Args[]
Type * getType() const
Definition: Value.h:215
static CastInst * CreateFPCast(Value *S, Type *Ty, const Twine &Name="", Instruction *InsertBefore=nullptr)
Create an FPExt, BitCast, or FPTrunc for fp -> fp casts.
Function * getCalledFunction() const
double fabs(double x);
void copyFastMathFlags(const Instruction *I)
Copy I's fast-math flags.
AttributeSet getAttributes() const
Return the attribute list for this Function.
Definition: Function.h:170
Value * getArgOperand(unsigned i) const
static BinaryOperator * Create(BinaryOps Op, Value *S1, Value *S2, const Twine &Name=Twine(), Instruction *InsertBefore=nullptr)
LLVM_ATTRIBUTE_UNUSED_RESULT std::enable_if< !is_simple_type< Y >::value, typename cast_retty< X, const Y >::ret_type >::type dyn_cast(const Y &Val)
Definition: Casting.h:265
StringRef getName(LibFunc::Func F) const
static bool isFNeg(const Value *V, bool IgnoreZeroSign=false)
#define I(x, y, z)
Definition: MD5.cpp:54
bool hasOneUse() const
Definition: Value.h:284
Type * getFloatTy()
Fetch the type representing a 32-bit floating point value.
Definition: IRBuilder.h:336
void setAttributes(const AttributeSet &Attrs)
Module * getParent()
Get the module that this global value is contained inside of...
Definition: GlobalValue.h:336
LLVM Value Representation.
Definition: Value.h:69
Instruction * EraseInstFromFunction(Instruction &I)
Definition: InstCombine.h:301
Value * CreateFPExt(Value *V, Type *DestTy, const Twine &Name="")
Definition: IRBuilder.h:1147
Value * CreateFPTrunc(Value *V, Type *DestTy, const Twine &Name="")
Definition: IRBuilder.h:1143
const BasicBlock * getParent() const
Definition: Instruction.h:53
Instruction * llvm::InstVisitor< InstCombiner , Instruction * >::visitFPTruncInst ( FPTruncInst I)
inlineinherited

Definition at line 185 of file InstVisitor.h.

185 { DELEGATE(CastInst);}
#define DELEGATE(CLASS_TO_VISIT)
Definition: InstVisitor.h:30
Instruction * InstCombiner::visitFree ( CallInst FI)

Definition at line 1896 of file InstructionCombining.cpp.

1896  {
1897  Value *Op = FI.getArgOperand(0);
1898 
1899  // free undef -> unreachable.
1900  if (isa<UndefValue>(Op)) {
1901  // Insert a new store to null because we cannot modify the CFG here.
1904  return EraseInstFromFunction(FI);
1905  }
1906 
1907  // If we have 'free null' delete the instruction. This can happen in stl code
1908  // when lots of inlining happens.
1909  if (isa<ConstantPointerNull>(Op))
1910  return EraseInstFromFunction(FI);
1911 
1912  // If we optimize for code size, try to move the call to free before the null
1913  // test so that simplify cfg can remove the empty block and dead code
1914  // elimination the branch. I.e., helps to turn something like:
1915  // if (foo) free(foo);
1916  // into
1917  // free(foo);
1918  if (MinimizeSize)
1920  return I;
1921 
1922  return nullptr;
1923 }
StoreInst * CreateStore(Value *Val, Value *Ptr, bool isVolatile=false)
Definition: IRBuilder.h:910
BuilderTy * Builder
Definition: InstCombine.h:102
static UndefValue * get(Type *T)
Definition: Constants.cpp:1372
LLVMContext & getContext() const
All values hold a context through their type.
Definition: Value.cpp:615
static PointerType * getInt1PtrTy(LLVMContext &C, unsigned AS=0)
Definition: Type.cpp:274
static ConstantInt * getTrue(LLVMContext &Context)
Definition: Constants.cpp:474
Value * getArgOperand(unsigned i) const
#define I(x, y, z)
Definition: MD5.cpp:54
LLVM Value Representation.
Definition: Value.h:69
Instruction * EraseInstFromFunction(Instruction &I)
Definition: InstCombine.h:301
static Instruction * tryToMoveFreeBeforeNullTest(CallInst &FI)
Move the call to free before a NULL test.
Instruction * InstCombiner::visitFRem ( BinaryOperator I)

Definition at line 1335 of file InstCombineMulDivRem.cpp.

1335  {
1336  Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
1337 
1338  if (Value *V = SimplifyVectorOp(I))
1339  return ReplaceInstUsesWith(I, V);
1340 
1341  if (Value *V = SimplifyFRemInst(Op0, Op1, DL))
1342  return ReplaceInstUsesWith(I, V);
1343 
1344  // Handle cases involving: rem X, (select Cond, Y, Z)
1345  if (isa<SelectInst>(Op1) && SimplifyDivRemOfSelect(I))
1346  return &I;
1347 
1348  return nullptr;
1349 }
bool SimplifyDivRemOfSelect(BinaryOperator &I)
Value * SimplifyVectorOp(BinaryOperator &Inst)
Makes transformation of binary operation specific for vector types.
Value * SimplifyFRemInst(Value *LHS, Value *RHS, const DataLayout *TD=nullptr, const TargetLibraryInfo *TLI=nullptr, const DominatorTree *DT=nullptr)
Instruction * ReplaceInstUsesWith(Instruction &I, Value *V)
Definition: InstCombine.h:282
Value * getOperand(unsigned i) const
Definition: User.h:90
#define I(x, y, z)
Definition: MD5.cpp:54
const DataLayout * DL
Definition: InstCombine.h:89
LLVM Value Representation.
Definition: Value.h:69
Instruction * InstCombiner::visitFSub ( BinaryOperator I)

Definition at line 1601 of file InstCombineAddSub.cpp.

1601  {
1602  Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
1603 
1604  if (Value *V = SimplifyVectorOp(I))
1605  return ReplaceInstUsesWith(I, V);
1606 
1607  if (Value *V = SimplifyFSubInst(Op0, Op1, I.getFastMathFlags(), DL))
1608  return ReplaceInstUsesWith(I, V);
1609 
1610  if (isa<Constant>(Op0))
1611  if (SelectInst *SI = dyn_cast<SelectInst>(Op1))
1612  if (Instruction *NV = FoldOpIntoSelect(I, SI))
1613  return NV;
1614 
1615  // If this is a 'B = x-(-A)', change to B = x+A, potentially looking
1616  // through FP extensions/truncations along the way.
1617  if (Value *V = dyn_castFNegVal(Op1)) {
1618  Instruction *NewI = BinaryOperator::CreateFAdd(Op0, V);
1619  NewI->copyFastMathFlags(&I);
1620  return NewI;
1621  }
1622  if (FPTruncInst *FPTI = dyn_cast<FPTruncInst>(Op1)) {
1623  if (Value *V = dyn_castFNegVal(FPTI->getOperand(0))) {
1624  Value *NewTrunc = Builder->CreateFPTrunc(V, I.getType());
1625  Instruction *NewI = BinaryOperator::CreateFAdd(Op0, NewTrunc);
1626  NewI->copyFastMathFlags(&I);
1627  return NewI;
1628  }
1629  } else if (FPExtInst *FPEI = dyn_cast<FPExtInst>(Op1)) {
1630  if (Value *V = dyn_castFNegVal(FPEI->getOperand(0))) {
1631  Value *NewExt = Builder->CreateFPExt(V, I.getType());
1632  Instruction *NewI = BinaryOperator::CreateFAdd(Op0, NewExt);
1633  NewI->copyFastMathFlags(&I);
1634  return NewI;
1635  }
1636  }
1637 
1638  if (I.hasUnsafeAlgebra()) {
1639  if (Value *V = FAddCombine(Builder).simplify(&I))
1640  return ReplaceInstUsesWith(I, V);
1641  }
1642 
1643  return nullptr;
1644 }
Value * SimplifyVectorOp(BinaryOperator &Inst)
Makes transformation of binary operation specific for vector types.
Instruction * FoldOpIntoSelect(Instruction &Op, SelectInst *SI)
FastMathFlags getFastMathFlags() const
bool hasUnsafeAlgebra() const
Determine whether the unsafe-algebra flag is set.
Instruction * ReplaceInstUsesWith(Instruction &I, Value *V)
Definition: InstCombine.h:282
Value * SimplifyFSubInst(Value *LHS, Value *RHS, FastMathFlags FMF, const DataLayout *TD=nullptr, const TargetLibraryInfo *TLI=nullptr, const DominatorTree *DT=nullptr)
Value * getOperand(unsigned i) const
Definition: User.h:90
BuilderTy * Builder
Definition: InstCombine.h:102
Type * getType() const
Definition: Value.h:215
void copyFastMathFlags(const Instruction *I)
Copy I's fast-math flags.
Value * dyn_castFNegVal(Value *V, bool NoSignedZero=false) const
const DataLayout * DL
Definition: InstCombine.h:89
This class represents a truncation of floating point types.
LLVM Value Representation.
Definition: Value.h:69
Value * CreateFPExt(Value *V, Type *DestTy, const Twine &Name="")
Definition: IRBuilder.h:1147
This class represents an extension of floating point types.
Value * CreateFPTrunc(Value *V, Type *DestTy, const Twine &Name="")
Definition: IRBuilder.h:1143
void llvm::InstVisitor< InstCombiner , Instruction * >::visitFunction ( Function F)
inlineinherited

Definition at line 142 of file InstVisitor.h.

142 {}
Instruction * InstCombiner::visitGetElementPtrInst ( GetElementPtrInst GEP)

See if we can simplify: X = bitcast A* to B* Y = gep X, <...constant indices...> into a gep of the original struct. This is important for SROA and alias analysis of unions. If "A" is also a bitcast, wait for A/X to be merged.

Definition at line 1284 of file InstructionCombining.cpp.

1284  {
1285  SmallVector<Value*, 8> Ops(GEP.op_begin(), GEP.op_end());
1286 
1287  if (Value *V = SimplifyGEPInst(Ops, DL))
1288  return ReplaceInstUsesWith(GEP, V);
1289 
1290  Value *PtrOp = GEP.getOperand(0);
1291 
1292  // Eliminate unneeded casts for indices, and replace indices which displace
1293  // by multiples of a zero size type with zero.
1294  if (DL) {
1295  bool MadeChange = false;
1296  Type *IntPtrTy = DL->getIntPtrType(GEP.getPointerOperandType());
1297 
1298  gep_type_iterator GTI = gep_type_begin(GEP);
1299  for (User::op_iterator I = GEP.op_begin() + 1, E = GEP.op_end();
1300  I != E; ++I, ++GTI) {
1301  // Skip indices into struct types.
1302  SequentialType *SeqTy = dyn_cast<SequentialType>(*GTI);
1303  if (!SeqTy) continue;
1304 
1305  // If the element type has zero size then any index over it is equivalent
1306  // to an index of zero, so replace it with zero if it is not zero already.
1307  if (SeqTy->getElementType()->isSized() &&
1308  DL->getTypeAllocSize(SeqTy->getElementType()) == 0)
1309  if (!isa<Constant>(*I) || !cast<Constant>(*I)->isNullValue()) {
1310  *I = Constant::getNullValue(IntPtrTy);
1311  MadeChange = true;
1312  }
1313 
1314  Type *IndexTy = (*I)->getType();
1315  if (IndexTy != IntPtrTy) {
1316  // If we are using a wider index than needed for this platform, shrink
1317  // it to what we need. If narrower, sign-extend it to what we need.
1318  // This explicit cast can make subsequent optimizations more obvious.
1319  *I = Builder->CreateIntCast(*I, IntPtrTy, true);
1320  MadeChange = true;
1321  }
1322  }
1323  if (MadeChange) return &GEP;
1324  }
1325 
1326  // Check to see if the inputs to the PHI node are getelementptr instructions.
1327  if (PHINode *PN = dyn_cast<PHINode>(PtrOp)) {
1328  GetElementPtrInst *Op1 = dyn_cast<GetElementPtrInst>(PN->getOperand(0));
1329  if (!Op1)
1330  return nullptr;
1331 
1332  signed DI = -1;
1333 
1334  for (auto I = PN->op_begin()+1, E = PN->op_end(); I !=E; ++I) {
1336  if (!Op2 || Op1->getNumOperands() != Op2->getNumOperands())
1337  return nullptr;
1338 
1339  // Keep track of the type as we walk the GEP.
1340  Type *CurTy = Op1->getOperand(0)->getType()->getScalarType();
1341 
1342  for (unsigned J = 0, F = Op1->getNumOperands(); J != F; ++J) {
1343  if (Op1->getOperand(J)->getType() != Op2->getOperand(J)->getType())
1344  return nullptr;
1345 
1346  if (Op1->getOperand(J) != Op2->getOperand(J)) {
1347  if (DI == -1) {
1348  // We have not seen any differences yet in the GEPs feeding the
1349  // PHI yet, so we record this one if it is allowed to be a
1350  // variable.
1351 
1352  // The first two arguments can vary for any GEP, the rest have to be
1353  // static for struct slots
1354  if (J > 1 && CurTy->isStructTy())
1355  return nullptr;
1356 
1357  DI = J;
1358  } else {
1359  // The GEP is different by more than one input. While this could be
1360  // extended to support GEPs that vary by more than one variable it
1361  // doesn't make sense since it greatly increases the complexity and
1362  // would result in an R+R+R addressing mode which no backend
1363  // directly supports and would need to be broken into several
1364  // simpler instructions anyway.
1365  return nullptr;
1366  }
1367  }
1368 
1369  // Sink down a layer of the type for the next iteration.
1370  if (J > 0) {
1371  if (CompositeType *CT = dyn_cast<CompositeType>(CurTy)) {
1372  CurTy = CT->getTypeAtIndex(Op1->getOperand(J));
1373  } else {
1374  CurTy = nullptr;
1375  }
1376  }
1377  }
1378  }
1379 
1380  GetElementPtrInst *NewGEP = cast<GetElementPtrInst>(Op1->clone());
1381 
1382  if (DI == -1) {
1383  // All the GEPs feeding the PHI are identical. Clone one down into our
1384  // BB so that it can be merged with the current GEP.
1386  NewGEP);
1387  } else {
1388  // All the GEPs feeding the PHI differ at a single offset. Clone a GEP
1389  // into the current block so it can be merged, and create a new PHI to
1390  // set that index.
1391  Instruction *InsertPt = Builder->GetInsertPoint();
1392  Builder->SetInsertPoint(PN);
1393  PHINode *NewPN = Builder->CreatePHI(Op1->getOperand(DI)->getType(),
1394  PN->getNumOperands());
1395  Builder->SetInsertPoint(InsertPt);
1396 
1397  for (auto &I : PN->operands())
1398  NewPN->addIncoming(cast<GEPOperator>(I)->getOperand(DI),
1399  PN->getIncomingBlock(I));
1400 
1401  NewGEP->setOperand(DI, NewPN);
1403  NewGEP);
1404  NewGEP->setOperand(DI, NewPN);
1405  }
1406 
1407  GEP.setOperand(0, NewGEP);
1408  PtrOp = NewGEP;
1409  }
1410 
1411  // Combine Indices - If the source pointer to this getelementptr instruction
1412  // is a getelementptr instruction, combine the indices of the two
1413  // getelementptr instructions into a single instruction.
1414  //
1415  if (GEPOperator *Src = dyn_cast<GEPOperator>(PtrOp)) {
1416  if (!shouldMergeGEPs(*cast<GEPOperator>(&GEP), *Src))
1417  return nullptr;
1418 
1419  // Note that if our source is a gep chain itself then we wait for that
1420  // chain to be resolved before we perform this transformation. This
1421  // avoids us creating a TON of code in some cases.
1422  if (GEPOperator *SrcGEP =
1423  dyn_cast<GEPOperator>(Src->getOperand(0)))
1424  if (SrcGEP->getNumOperands() == 2 && shouldMergeGEPs(*Src, *SrcGEP))
1425  return nullptr; // Wait until our source is folded to completion.
1426 
1427  SmallVector<Value*, 8> Indices;
1428 
1429  // Find out whether the last index in the source GEP is a sequential idx.
1430  bool EndsWithSequential = false;
1431  for (gep_type_iterator I = gep_type_begin(*Src), E = gep_type_end(*Src);
1432  I != E; ++I)
1433  EndsWithSequential = !(*I)->isStructTy();
1434 
1435  // Can we combine the two pointer arithmetics offsets?
1436  if (EndsWithSequential) {
1437  // Replace: gep (gep %P, long B), long A, ...
1438  // With: T = long A+B; gep %P, T, ...
1439  //
1440  Value *Sum;
1441  Value *SO1 = Src->getOperand(Src->getNumOperands()-1);
1442  Value *GO1 = GEP.getOperand(1);
1443  if (SO1 == Constant::getNullValue(SO1->getType())) {
1444  Sum = GO1;
1445  } else if (GO1 == Constant::getNullValue(GO1->getType())) {
1446  Sum = SO1;
1447  } else {
1448  // If they aren't the same type, then the input hasn't been processed
1449  // by the loop above yet (which canonicalizes sequential index types to
1450  // intptr_t). Just avoid transforming this until the input has been
1451  // normalized.
1452  if (SO1->getType() != GO1->getType())
1453  return nullptr;
1454  Sum = Builder->CreateAdd(SO1, GO1, PtrOp->getName()+".sum");
1455  }
1456 
1457  // Update the GEP in place if possible.
1458  if (Src->getNumOperands() == 2) {
1459  GEP.setOperand(0, Src->getOperand(0));
1460  GEP.setOperand(1, Sum);
1461  return &GEP;
1462  }
1463  Indices.append(Src->op_begin()+1, Src->op_end()-1);
1464  Indices.push_back(Sum);
1465  Indices.append(GEP.op_begin()+2, GEP.op_end());
1466  } else if (isa<Constant>(*GEP.idx_begin()) &&
1467  cast<Constant>(*GEP.idx_begin())->isNullValue() &&
1468  Src->getNumOperands() != 1) {
1469  // Otherwise we can do the fold if the first index of the GEP is a zero
1470  Indices.append(Src->op_begin()+1, Src->op_end());
1471  Indices.append(GEP.idx_begin()+1, GEP.idx_end());
1472  }
1473 
1474  if (!Indices.empty())
1475  return (GEP.isInBounds() && Src->isInBounds()) ?
1476  GetElementPtrInst::CreateInBounds(Src->getOperand(0), Indices,
1477  GEP.getName()) :
1478  GetElementPtrInst::Create(Src->getOperand(0), Indices, GEP.getName());
1479  }
1480 
1481  // Canonicalize (gep i8* X, -(ptrtoint Y)) to (sub (ptrtoint X), (ptrtoint Y))
1482  // The GEP pattern is emitted by the SCEV expander for certain kinds of
1483  // pointer arithmetic.
1484  if (DL && GEP.getNumIndices() == 1 &&
1485  match(GEP.getOperand(1), m_Neg(m_PtrToInt(m_Value())))) {
1486  unsigned AS = GEP.getPointerAddressSpace();
1487  if (GEP.getType() == Builder->getInt8PtrTy(AS) &&
1488  GEP.getOperand(1)->getType()->getScalarSizeInBits() ==
1489  DL->getPointerSizeInBits(AS)) {
1490  Operator *Index = cast<Operator>(GEP.getOperand(1));
1491  Value *PtrToInt = Builder->CreatePtrToInt(PtrOp, Index->getType());
1492  Value *NewSub = Builder->CreateSub(PtrToInt, Index->getOperand(1));
1493  return CastInst::Create(Instruction::IntToPtr, NewSub, GEP.getType());
1494  }
1495  }
1496 
1497  // Handle gep(bitcast x) and gep(gep x, 0, 0, 0).
1498  Value *StrippedPtr = PtrOp->stripPointerCasts();
1499  PointerType *StrippedPtrTy = dyn_cast<PointerType>(StrippedPtr->getType());
1500 
1501  // We do not handle pointer-vector geps here.
1502  if (!StrippedPtrTy)
1503  return nullptr;
1504 
1505  if (StrippedPtr != PtrOp) {
1506  bool HasZeroPointerIndex = false;
1507  if (ConstantInt *C = dyn_cast<ConstantInt>(GEP.getOperand(1)))
1508  HasZeroPointerIndex = C->isZero();
1509 
1510  // Transform: GEP (bitcast [10 x i8]* X to [0 x i8]*), i32 0, ...
1511  // into : GEP [10 x i8]* X, i32 0, ...
1512  //
1513  // Likewise, transform: GEP (bitcast i8* X to [0 x i8]*), i32 0, ...
1514  // into : GEP i8* X, ...
1515  //
1516  // This occurs when the program declares an array extern like "int X[];"
1517  if (HasZeroPointerIndex) {
1518  PointerType *CPTy = cast<PointerType>(PtrOp->getType());
1519  if (ArrayType *CATy =
1520  dyn_cast<ArrayType>(CPTy->getElementType())) {
1521  // GEP (bitcast i8* X to [0 x i8]*), i32 0, ... ?
1522  if (CATy->getElementType() == StrippedPtrTy->getElementType()) {
1523  // -> GEP i8* X, ...
1524  SmallVector<Value*, 8> Idx(GEP.idx_begin()+1, GEP.idx_end());
1525  GetElementPtrInst *Res =
1526  GetElementPtrInst::Create(StrippedPtr, Idx, GEP.getName());
1527  Res->setIsInBounds(GEP.isInBounds());
1528  if (StrippedPtrTy->getAddressSpace() == GEP.getAddressSpace())
1529  return Res;
1530  // Insert Res, and create an addrspacecast.
1531  // e.g.,
1532  // GEP (addrspacecast i8 addrspace(1)* X to [0 x i8]*), i32 0, ...
1533  // ->
1534  // %0 = GEP i8 addrspace(1)* X, ...
1535  // addrspacecast i8 addrspace(1)* %0 to i8*
1536  return new AddrSpaceCastInst(Builder->Insert(Res), GEP.getType());
1537  }
1538 
1539  if (ArrayType *XATy =
1540  dyn_cast<ArrayType>(StrippedPtrTy->getElementType())){
1541  // GEP (bitcast [10 x i8]* X to [0 x i8]*), i32 0, ... ?
1542  if (CATy->getElementType() == XATy->getElementType()) {
1543  // -> GEP [10 x i8]* X, i32 0, ...
1544  // At this point, we know that the cast source type is a pointer
1545  // to an array of the same type as the destination pointer
1546  // array. Because the array type is never stepped over (there
1547  // is a leading zero) we can fold the cast into this GEP.
1548  if (StrippedPtrTy->getAddressSpace() == GEP.getAddressSpace()) {
1549  GEP.setOperand(0, StrippedPtr);
1550  return &GEP;
1551  }
1552  // Cannot replace the base pointer directly because StrippedPtr's
1553  // address space is different. Instead, create a new GEP followed by
1554  // an addrspacecast.
1555  // e.g.,
1556  // GEP (addrspacecast [10 x i8] addrspace(1)* X to [0 x i8]*),
1557  // i32 0, ...
1558  // ->
1559  // %0 = GEP [10 x i8] addrspace(1)* X, ...
1560  // addrspacecast i8 addrspace(1)* %0 to i8*
1561  SmallVector<Value*, 8> Idx(GEP.idx_begin(), GEP.idx_end());
1562  Value *NewGEP = GEP.isInBounds() ?
1563  Builder->CreateInBoundsGEP(StrippedPtr, Idx, GEP.getName()) :
1564  Builder->CreateGEP(StrippedPtr, Idx, GEP.getName());
1565  return new AddrSpaceCastInst(NewGEP, GEP.getType());
1566  }
1567  }
1568  }
1569  } else if (GEP.getNumOperands() == 2) {
1570  // Transform things like:
1571  // %t = getelementptr i32* bitcast ([2 x i32]* %str to i32*), i32 %V
1572  // into: %t1 = getelementptr [2 x i32]* %str, i32 0, i32 %V; bitcast
1573  Type *SrcElTy = StrippedPtrTy->getElementType();
1574  Type *ResElTy = PtrOp->getType()->getPointerElementType();
1575  if (DL && SrcElTy->isArrayTy() &&
1576  DL->getTypeAllocSize(SrcElTy->getArrayElementType()) ==
1577  DL->getTypeAllocSize(ResElTy)) {
1578  Type *IdxType = DL->getIntPtrType(GEP.getType());
1579  Value *Idx[2] = { Constant::getNullValue(IdxType), GEP.getOperand(1) };
1580  Value *NewGEP = GEP.isInBounds() ?
1581  Builder->CreateInBoundsGEP(StrippedPtr, Idx, GEP.getName()) :
1582  Builder->CreateGEP(StrippedPtr, Idx, GEP.getName());
1583 
1584  // V and GEP are both pointer types --> BitCast
1586  GEP.getType());
1587  }
1588 
1589  // Transform things like:
1590  // %V = mul i64 %N, 4
1591  // %t = getelementptr i8* bitcast (i32* %arr to i8*), i32 %V
1592  // into: %t1 = getelementptr i32* %arr, i32 %N; bitcast
1593  if (DL && ResElTy->isSized() && SrcElTy->isSized()) {
1594  // Check that changing the type amounts to dividing the index by a scale
1595  // factor.
1596  uint64_t ResSize = DL->getTypeAllocSize(ResElTy);
1597  uint64_t SrcSize = DL->getTypeAllocSize(SrcElTy);
1598  if (ResSize && SrcSize % ResSize == 0) {
1599  Value *Idx = GEP.getOperand(1);
1600  unsigned BitWidth = Idx->getType()->getPrimitiveSizeInBits();
1601  uint64_t Scale = SrcSize / ResSize;
1602 
1603  // Earlier transforms ensure that the index has type IntPtrType, which
1604  // considerably simplifies the logic by eliminating implicit casts.
1605  assert(Idx->getType() == DL->getIntPtrType(GEP.getType()) &&
1606  "Index not cast to pointer width?");
1607 
1608  bool NSW;
1609  if (Value *NewIdx = Descale(Idx, APInt(BitWidth, Scale), NSW)) {
1610  // Successfully decomposed Idx as NewIdx * Scale, form a new GEP.
1611  // If the multiplication NewIdx * Scale may overflow then the new
1612  // GEP may not be "inbounds".
1613  Value *NewGEP = GEP.isInBounds() && NSW ?
1614  Builder->CreateInBoundsGEP(StrippedPtr, NewIdx, GEP.getName()) :
1615  Builder->CreateGEP(StrippedPtr, NewIdx, GEP.getName());
1616 
1617  // The NewGEP must be pointer typed, so must the old one -> BitCast
1619  GEP.getType());
1620  }
1621  }
1622  }
1623 
1624  // Similarly, transform things like:
1625  // getelementptr i8* bitcast ([100 x double]* X to i8*), i32 %tmp
1626  // (where tmp = 8*tmp2) into:
1627  // getelementptr [100 x double]* %arr, i32 0, i32 %tmp2; bitcast
1628  if (DL && ResElTy->isSized() && SrcElTy->isSized() &&
1629  SrcElTy->isArrayTy()) {
1630  // Check that changing to the array element type amounts to dividing the
1631  // index by a scale factor.
1632  uint64_t ResSize = DL->getTypeAllocSize(ResElTy);
1633  uint64_t ArrayEltSize
1634  = DL->getTypeAllocSize(SrcElTy->getArrayElementType());
1635  if (ResSize && ArrayEltSize % ResSize == 0) {
1636  Value *Idx = GEP.getOperand(1);
1637  unsigned BitWidth = Idx->getType()->getPrimitiveSizeInBits();
1638  uint64_t Scale = ArrayEltSize / ResSize;
1639 
1640  // Earlier transforms ensure that the index has type IntPtrType, which
1641  // considerably simplifies the logic by eliminating implicit casts.
1642  assert(Idx->getType() == DL->getIntPtrType(GEP.getType()) &&
1643  "Index not cast to pointer width?");
1644 
1645  bool NSW;
1646  if (Value *NewIdx = Descale(Idx, APInt(BitWidth, Scale), NSW)) {
1647  // Successfully decomposed Idx as NewIdx * Scale, form a new GEP.
1648  // If the multiplication NewIdx * Scale may overflow then the new
1649  // GEP may not be "inbounds".
1650  Value *Off[2] = {
1652  NewIdx
1653  };
1654 
1655  Value *NewGEP = GEP.isInBounds() && NSW ?
1656  Builder->CreateInBoundsGEP(StrippedPtr, Off, GEP.getName()) :
1657  Builder->CreateGEP(StrippedPtr, Off, GEP.getName());
1658  // The NewGEP must be pointer typed, so must the old one -> BitCast
1660  GEP.getType());
1661  }
1662  }
1663  }
1664  }
1665  }
1666 
1667  if (!DL)
1668  return nullptr;
1669 
1670  /// See if we can simplify:
1671  /// X = bitcast A* to B*
1672  /// Y = gep X, <...constant indices...>
1673  /// into a gep of the original struct. This is important for SROA and alias
1674  /// analysis of unions. If "A" is also a bitcast, wait for A/X to be merged.
1675  if (BitCastInst *BCI = dyn_cast<BitCastInst>(PtrOp)) {
1676  Value *Operand = BCI->getOperand(0);
1677  PointerType *OpType = cast<PointerType>(Operand->getType());
1678  unsigned OffsetBits = DL->getPointerTypeSizeInBits(OpType);
1679  APInt Offset(OffsetBits, 0);
1680  if (!isa<BitCastInst>(Operand) &&
1681  GEP.accumulateConstantOffset(*DL, Offset) &&
1682  StrippedPtrTy->getAddressSpace() == GEP.getPointerAddressSpace()) {
1683 
1684  // If this GEP instruction doesn't move the pointer, just replace the GEP
1685  // with a bitcast of the real input to the dest type.
1686  if (!Offset) {
1687  // If the bitcast is of an allocation, and the allocation will be
1688  // converted to match the type of the cast, don't touch this.
1689  if (isa<AllocaInst>(Operand) || isAllocationFn(Operand, TLI)) {
1690  // See if the bitcast simplifies, if so, don't nuke this GEP yet.
1691  if (Instruction *I = visitBitCast(*BCI)) {
1692  if (I != BCI) {
1693  I->takeName(BCI);
1694  BCI->getParent()->getInstList().insert(BCI, I);
1695  ReplaceInstUsesWith(*BCI, I);
1696  }
1697  return &GEP;
1698  }
1699  }
1700  return new BitCastInst(Operand, GEP.getType());
1701  }
1702 
1703  // Otherwise, if the offset is non-zero, we need to find out if there is a
1704  // field at Offset in 'A's type. If so, we can pull the cast through the
1705  // GEP.
1706  SmallVector<Value*, 8> NewIndices;
1707  if (FindElementAtOffset(OpType, Offset.getSExtValue(), NewIndices)) {
1708  Value *NGEP = GEP.isInBounds() ?
1709  Builder->CreateInBoundsGEP(Operand, NewIndices) :
1710  Builder->CreateGEP(Operand, NewIndices);
1711 
1712  if (NGEP->getType() == GEP.getType())
1713  return ReplaceInstUsesWith(GEP, NGEP);
1714  NGEP->takeName(&GEP);
1715  return new BitCastInst(NGEP, GEP.getType());
1716  }
1717  }
1718  }
1719 
1720  return nullptr;
1721 }
Value * CreateGEP(Value *Ptr, ArrayRef< Value * > IdxList, const Twine &Name="")
Definition: IRBuilder.h:957
bool isAllocationFn(const Value *V, const TargetLibraryInfo *TLI, bool LookThroughBitCast=false)
Tests if a value is a call or invoke to a library function that allocates or reallocates memory (eith...
class_match< Value > m_Value()
m_Value() - Match an arbitrary value and ignore it.
Definition: PatternMatch.h:70
BasicBlock::iterator GetInsertPoint() const
Definition: IRBuilder.h:78
void addIncoming(Value *V, BasicBlock *BB)
Value * CreateIntCast(Value *V, Type *DestTy, bool isSigned, const Twine &Name="")
Definition: IRBuilder.h:1221
Instruction * visitBitCast(BitCastInst &CI)
unsigned getNumOperands() const
Definition: User.h:110
unsigned getPointerTypeSizeInBits(Type *) const
Definition: DataLayout.cpp:611
unsigned getPointerAddressSpace() const
Returns the address space of the pointer operand.
Definition: Instructions.h:873
gep_type_iterator gep_type_end(const User *GEP)
F(f)
unsigned getAddressSpace() const
Return the address space of the Pointer type.
Definition: DerivedTypes.h:465
unsigned getNumIndices() const
Definition: Instructions.h:893
op_iterator op_begin()
Definition: User.h:120
Value * CreateSub(Value *LHS, Value *RHS, const Twine &Name="", bool HasNUW=false, bool HasNSW=false)
Definition: IRBuilder.h:654
InstTy * Insert(InstTy *I, const Twine &Name="") const
Insert and return the specified instruction.
Definition: IRBuilder.h:492
Type * getPointerElementType() const
Definition: Type.h:363
static Constant * getNullValue(Type *Ty)
Definition: Constants.cpp:133
StringRef getName() const
Definition: Value.cpp:168
bool match(Val *V, const Pattern &P)
Definition: PatternMatch.h:42
TargetLibraryInfo * TLI
Definition: InstCombine.h:90
PHINode * CreatePHI(Type *Ty, unsigned NumReservedValues, const Twine &Name="")
Definition: IRBuilder.h:1340
This class represents a conversion between pointers from one address space to another.
Type * getArrayElementType() const
Definition: Type.h:358
A Use represents the edge between a Value definition and its users.
Definition: Use.h:69
Instruction * getFirstNonPHI()
Returns a pointer to the first instruction in this block that is not a PHINode instruction.
Definition: BasicBlock.cpp:134
Instruction * clone() const
Type * getPointerOperandType() const
Definition: Instructions.h:868
static GetElementPtrInst * CreateInBounds(Value *Ptr, ArrayRef< Value * > IdxList, const Twine &NameStr="", Instruction *InsertBefore=nullptr)
Definition: Instructions.h:809
bool LLVM_ATTRIBUTE_UNUSED_RESULT empty() const
Definition: SmallVector.h:57
This class represents a no-op cast from one type to another.
op_iterator idx_begin()
Definition: Instructions.h:851
bool isArrayTy() const
Definition: Type.h:213
void takeName(Value *V)
Definition: Value.cpp:242
Type * getElementType() const
Definition: DerivedTypes.h:319
void SetInsertPoint(BasicBlock *TheBB)
This specifies that created instructions should be appended to the end of the specified block...
Definition: IRBuilder.h:83
bool isInBounds() const
isInBounds - Determine whether the GEP has the inbounds flag.
assert(Globals.size() > 1)
Value * Descale(Value *Val, APInt Scale, bool &NoSignedWrap)
Value * CreateAdd(Value *LHS, Value *RHS, const Twine &Name="", bool HasNUW=false, bool HasNSW=false)
Definition: IRBuilder.h:632
Value * CreatePtrToInt(Value *V, Type *DestTy, const Twine &Name="")
Definition: IRBuilder.h:1150
Instruction * ReplaceInstUsesWith(Instruction &I, Value *V)
Definition: InstCombine.h:282
op_iterator op_end()
Definition: User.h:122
const InstListType & getInstList() const
Return the underlying instruction list container.
Definition: BasicBlock.h:219
iterator insert(iterator where, NodeTy *New)
Definition: ilist.h:412
Value * getOperand(unsigned i) const
Definition: User.h:90
static CastInst * CreatePointerBitCastOrAddrSpaceCast(Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd)
Create a BitCast or an AddrSpaceCast cast instruction.
Value * SimplifyGEPInst(ArrayRef< Value * > Ops, const DataLayout *TD=nullptr, const TargetLibraryInfo *TLI=nullptr, const DominatorTree *DT=nullptr)
BuilderTy * Builder
Definition: InstCombine.h:102
void append(in_iter in_start, in_iter in_end)
Definition: SmallVector.h:417
Value * CreateInBoundsGEP(Value *Ptr, ArrayRef< Value * > IdxList, const Twine &Name="")
Definition: IRBuilder.h:970
PointerType * getInt8PtrTy(unsigned AddrSpace=0)
Fetch the type representing a pointer to an 8-bit integer value.
Definition: IRBuilder.h:351
static GetElementPtrInst * Create(Value *Ptr, ArrayRef< Value * > IdxList, const Twine &NameStr="", Instruction *InsertBefore=nullptr)
Definition: Instructions.h:792
unsigned getAddressSpace() const
Returns the address space of this instruction's pointer type.
Definition: Instructions.h:835
neg_match< LHS > m_Neg(const LHS &L)
m_Neg - Match an integer negate.
Definition: PatternMatch.h:854
IntegerType * getIntPtrType(LLVMContext &C, unsigned AddressSpace=0) const
Definition: DataLayout.cpp:703
Class for constant integers.
Definition: Constants.h:51
uint64_t getTypeAllocSize(Type *Ty) const
Definition: DataLayout.h:368
unsigned getScalarSizeInBits() const LLVM_READONLY
Definition: Type.cpp:135
Type * getType() const
Definition: Value.h:215
SequentialType * getType() const
Definition: Instructions.h:830
Value * stripPointerCasts()
Strips off any unneeded pointer casts, all-zero GEPs and aliases from the specified value...
Definition: Value.cpp:423
void setOperand(unsigned i, Value *Val)
Definition: User.h:94
bool accumulateConstantOffset(const DataLayout &DL, APInt &Offset) const
Accumulate the constant address offset of this GEP if possible.
Class for arbitrary precision integers.
Definition: APInt.h:75
bool isSized(SmallPtrSet< const Type *, 4 > *Visited=nullptr) const
Definition: Type.h:268
CastClass_match< OpTy, Instruction::PtrToInt > m_PtrToInt(const OpTy &Op)
m_PtrToInt
Definition: PatternMatch.h:761
LLVM_ATTRIBUTE_UNUSED_RESULT std::enable_if< !is_simple_type< Y >::value, typename cast_retty< X, const Y >::ret_type >::type dyn_cast(const Y &Val)
Definition: Casting.h:265
const Type * getScalarType() const LLVM_READONLY
Definition: Type.cpp:51
bool isStructTy() const
Definition: Type.h:209
static CastInst * Create(Instruction::CastOps, Value *S, Type *Ty, const Twine &Name="", Instruction *InsertBefore=nullptr)
Construct any of the CastInst subclasses.
#define I(x, y, z)
Definition: MD5.cpp:54
unsigned getPointerSizeInBits(unsigned AS=0) const
Definition: DataLayout.h:313
const DataLayout * DL
Definition: InstCombine.h:89
unsigned getPrimitiveSizeInBits() const LLVM_READONLY
Definition: Type.cpp:117
LLVM Value Representation.
Definition: Value.h:69
static bool shouldMergeGEPs(GEPOperator &GEP, GEPOperator &Src)
const BasicBlock * getParent() const
Definition: Instruction.h:53
Type * FindElementAtOffset(Type *PtrTy, int64_t Offset, SmallVectorImpl< Value * > &NewIndices)
gep_type_iterator gep_type_begin(const User *GEP)
Instruction * InstCombiner::visitICmpInst ( ICmpInst I)

Orders the operands of the compare so that they are listed from most complex to least complex. This puts constants before unary operators, before binary operators.

Definition at line 2327 of file InstCombineCompares.cpp.

2327  {
2328  bool Changed = false;
2329  Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
2330  unsigned Op0Cplxity = getComplexity(Op0);
2331  unsigned Op1Cplxity = getComplexity(Op1);
2332 
2333  /// Orders the operands of the compare so that they are listed from most
2334  /// complex to least complex. This puts constants before unary operators,
2335  /// before binary operators.
2336  if (Op0Cplxity < Op1Cplxity ||
2337  (Op0Cplxity == Op1Cplxity &&
2338  swapMayExposeCSEOpportunities(Op0, Op1))) {
2339  I.swapOperands();
2340  std::swap(Op0, Op1);
2341  Changed = true;
2342  }
2343 
2344  if (Value *V = SimplifyICmpInst(I.getPredicate(), Op0, Op1, DL))
2345  return ReplaceInstUsesWith(I, V);
2346 
2347  // comparing -val or val with non-zero is the same as just comparing val
2348  // ie, abs(val) != 0 -> val != 0
2349  if (I.getPredicate() == ICmpInst::ICMP_NE && match(Op1, m_Zero()))
2350  {
2351  Value *Cond, *SelectTrue, *SelectFalse;
2352  if (match(Op0, m_Select(m_Value(Cond), m_Value(SelectTrue),
2353  m_Value(SelectFalse)))) {
2354  if (Value *V = dyn_castNegVal(SelectTrue)) {
2355  if (V == SelectFalse)
2356  return CmpInst::Create(Instruction::ICmp, I.getPredicate(), V, Op1);
2357  }
2358  else if (Value *V = dyn_castNegVal(SelectFalse)) {
2359  if (V == SelectTrue)
2360  return CmpInst::Create(Instruction::ICmp, I.getPredicate(), V, Op1);
2361  }
2362  }
2363  }
2364 
2365  Type *Ty = Op0->getType();
2366 
2367  // icmp's with boolean values can always be turned into bitwise operations
2368  if (Ty->isIntegerTy(1)) {
2369  switch (I.getPredicate()) {
2370  default: llvm_unreachable("Invalid icmp instruction!");
2371  case ICmpInst::ICMP_EQ: { // icmp eq i1 A, B -> ~(A^B)
2372  Value *Xor = Builder->CreateXor(Op0, Op1, I.getName()+"tmp");
2373  return BinaryOperator::CreateNot(Xor);
2374  }
2375  case ICmpInst::ICMP_NE: // icmp eq i1 A, B -> A^B
2376  return BinaryOperator::CreateXor(Op0, Op1);
2377 
2378  case ICmpInst::ICMP_UGT:
2379  std::swap(Op0, Op1); // Change icmp ugt -> icmp ult
2380  // FALL THROUGH
2381  case ICmpInst::ICMP_ULT:{ // icmp ult i1 A, B -> ~A & B
2382  Value *Not = Builder->CreateNot(Op0, I.getName()+"tmp");
2383  return BinaryOperator::CreateAnd(Not, Op1);
2384  }
2385  case ICmpInst::ICMP_SGT:
2386  std::swap(Op0, Op1); // Change icmp sgt -> icmp slt
2387  // FALL THROUGH
2388  case ICmpInst::ICMP_SLT: { // icmp slt i1 A, B -> A & ~B
2389  Value *Not = Builder->CreateNot(Op1, I.getName()+"tmp");
2390  return BinaryOperator::CreateAnd(Not, Op0);
2391  }
2392  case ICmpInst::ICMP_UGE:
2393  std::swap(Op0, Op1); // Change icmp uge -> icmp ule
2394  // FALL THROUGH
2395  case ICmpInst::ICMP_ULE: { // icmp ule i1 A, B -> ~A | B
2396  Value *Not = Builder->CreateNot(Op0, I.getName()+"tmp");
2397  return BinaryOperator::CreateOr(Not, Op1);
2398  }
2399  case ICmpInst::ICMP_SGE:
2400  std::swap(Op0, Op1); // Change icmp sge -> icmp sle
2401  // FALL THROUGH
2402  case ICmpInst::ICMP_SLE: { // icmp sle i1 A, B -> A | ~B
2403  Value *Not = Builder->CreateNot(Op1, I.getName()+"tmp");
2404  return BinaryOperator::CreateOr(Not, Op0);
2405  }
2406  }
2407  }
2408 
2409  unsigned BitWidth = 0;
2410  if (Ty->isIntOrIntVectorTy())
2411  BitWidth = Ty->getScalarSizeInBits();
2412  else if (DL) // Pointers require DL info to get their size.
2413  BitWidth = DL->getTypeSizeInBits(Ty->getScalarType());
2414 
2415  bool isSignBit = false;
2416 
2417  // See if we are doing a comparison with a constant.
2418  if (ConstantInt *CI = dyn_cast<ConstantInt>(Op1)) {
2419  Value *A = nullptr, *B = nullptr;
2420 
2421  // Match the following pattern, which is a common idiom when writing
2422  // overflow-safe integer arithmetic function. The source performs an
2423  // addition in wider type, and explicitly checks for overflow using
2424  // comparisons against INT_MIN and INT_MAX. Simplify this by using the
2425  // sadd_with_overflow intrinsic.
2426  //
2427  // TODO: This could probably be generalized to handle other overflow-safe
2428  // operations if we worked out the formulas to compute the appropriate
2429  // magic constants.
2430  //
2431  // sum = a + b
2432  // if (sum+128 >u 255) ... -> llvm.sadd.with.overflow.i8
2433  {
2434  ConstantInt *CI2; // I = icmp ugt (add (add A, B), CI2), CI
2435  if (I.getPredicate() == ICmpInst::ICMP_UGT &&
2436  match(Op0, m_Add(m_Add(m_Value(A), m_Value(B)), m_ConstantInt(CI2))))
2437  if (Instruction *Res = ProcessUGT_ADDCST_ADD(I, A, B, CI2, CI, *this))
2438  return Res;
2439  }
2440 
2441  // (icmp ne/eq (sub A B) 0) -> (icmp ne/eq A, B)
2442  if (I.isEquality() && CI->isZero() &&
2443  match(Op0, m_Sub(m_Value(A), m_Value(B)))) {
2444  // (icmp cond A B) if cond is equality
2445  return new ICmpInst(I.getPredicate(), A, B);
2446  }
2447 
2448  // If we have an icmp le or icmp ge instruction, turn it into the
2449  // appropriate icmp lt or icmp gt instruction. This allows us to rely on
2450  // them being folded in the code below. The SimplifyICmpInst code has
2451  // already handled the edge cases for us, so we just assert on them.
2452  switch (I.getPredicate()) {
2453  default: break;
2454  case ICmpInst::ICMP_ULE:
2455  assert(!CI->isMaxValue(false)); // A <=u MAX -> TRUE
2456  return new ICmpInst(ICmpInst::ICMP_ULT, Op0,
2457  Builder->getInt(CI->getValue()+1));
2458  case ICmpInst::ICMP_SLE:
2459  assert(!CI->isMaxValue(true)); // A <=s MAX -> TRUE
2460  return new ICmpInst(ICmpInst::ICMP_SLT, Op0,
2461  Builder->getInt(CI->getValue()+1));
2462  case ICmpInst::ICMP_UGE:
2463  assert(!CI->isMinValue(false)); // A >=u MIN -> TRUE
2464  return new ICmpInst(ICmpInst::ICMP_UGT, Op0,
2465  Builder->getInt(CI->getValue()-1));
2466  case ICmpInst::ICMP_SGE:
2467  assert(!CI->isMinValue(true)); // A >=s MIN -> TRUE
2468  return new ICmpInst(ICmpInst::ICMP_SGT, Op0,
2469  Builder->getInt(CI->getValue()-1));
2470  }
2471 
2472  // If this comparison is a normal comparison, it demands all
2473  // bits, if it is a sign bit comparison, it only demands the sign bit.
2474  bool UnusedBit;
2475  isSignBit = isSignBitCheck(I.getPredicate(), CI, UnusedBit);
2476  }
2477 
2478  // See if we can fold the comparison based on range information we can get
2479  // by checking whether bits are known to be zero or one in the input.
2480  if (BitWidth != 0) {
2481  APInt Op0KnownZero(BitWidth, 0), Op0KnownOne(BitWidth, 0);
2482  APInt Op1KnownZero(BitWidth, 0), Op1KnownOne(BitWidth, 0);
2483 
2485  DemandedBitsLHSMask(I, BitWidth, isSignBit),
2486  Op0KnownZero, Op0KnownOne, 0))
2487  return &I;
2489  APInt::getAllOnesValue(BitWidth),
2490  Op1KnownZero, Op1KnownOne, 0))
2491  return &I;
2492 
2493  // Given the known and unknown bits, compute a range that the LHS could be
2494  // in. Compute the Min, Max and RHS values based on the known bits. For the
2495  // EQ and NE we use unsigned values.
2496  APInt Op0Min(BitWidth, 0), Op0Max(BitWidth, 0);
2497  APInt Op1Min(BitWidth, 0), Op1Max(BitWidth, 0);
2498  if (I.isSigned()) {
2499  ComputeSignedMinMaxValuesFromKnownBits(Op0KnownZero, Op0KnownOne,
2500  Op0Min, Op0Max);
2501  ComputeSignedMinMaxValuesFromKnownBits(Op1KnownZero, Op1KnownOne,
2502  Op1Min, Op1Max);
2503  } else {
2504  ComputeUnsignedMinMaxValuesFromKnownBits(Op0KnownZero, Op0KnownOne,
2505  Op0Min, Op0Max);
2506  ComputeUnsignedMinMaxValuesFromKnownBits(Op1KnownZero, Op1KnownOne,
2507  Op1Min, Op1Max);
2508  }
2509 
2510  // If Min and Max are known to be the same, then SimplifyDemandedBits
2511  // figured out that the LHS is a constant. Just constant fold this now so
2512  // that code below can assume that Min != Max.
2513  if (!isa<Constant>(Op0) && Op0Min == Op0Max)
2514  return new ICmpInst(I.getPredicate(),
2515  ConstantInt::get(Op0->getType(), Op0Min), Op1);
2516  if (!isa<Constant>(Op1) && Op1Min == Op1Max)
2517  return new ICmpInst(I.getPredicate(), Op0,
2518  ConstantInt::get(Op1->getType(), Op1Min));
2519 
2520  // Based on the range information we know about the LHS, see if we can
2521  // simplify this comparison. For example, (x&4) < 8 is always true.
2522  switch (I.getPredicate()) {
2523  default: llvm_unreachable("Unknown icmp opcode!");
2524  case ICmpInst::ICMP_EQ: {
2525  if (Op0Max.ult(Op1Min) || Op0Min.ugt(Op1Max))
2527 
2528  // If all bits are known zero except for one, then we know at most one
2529  // bit is set. If the comparison is against zero, then this is a check
2530  // to see if *that* bit is set.
2531  APInt Op0KnownZeroInverted = ~Op0KnownZero;
2532  if (~Op1KnownZero == 0) {
2533  // If the LHS is an AND with the same constant, look through it.
2534  Value *LHS = nullptr;
2535  ConstantInt *LHSC = nullptr;
2536  if (!match(Op0, m_And(m_Value(LHS), m_ConstantInt(LHSC))) ||
2537  LHSC->getValue() != Op0KnownZeroInverted)
2538  LHS = Op0;
2539 
2540  // If the LHS is 1 << x, and we know the result is a power of 2 like 8,
2541  // then turn "((1 << x)&8) == 0" into "x != 3".
2542  // or turn "((1 << x)&7) == 0" into "x > 2".
2543  Value *X = nullptr;
2544  if (match(LHS, m_Shl(m_One(), m_Value(X)))) {
2545  APInt ValToCheck = Op0KnownZeroInverted;
2546  if (ValToCheck.isPowerOf2()) {
2547  unsigned CmpVal = ValToCheck.countTrailingZeros();
2548  return new ICmpInst(ICmpInst::ICMP_NE, X,
2549  ConstantInt::get(X->getType(), CmpVal));
2550  } else if ((++ValToCheck).isPowerOf2()) {
2551  unsigned CmpVal = ValToCheck.countTrailingZeros() - 1;
2552  return new ICmpInst(ICmpInst::ICMP_UGT, X,
2553  ConstantInt::get(X->getType(), CmpVal));
2554  }
2555  }
2556 
2557  // If the LHS is 8 >>u x, and we know the result is a power of 2 like 1,
2558  // then turn "((8 >>u x)&1) == 0" into "x != 3".
2559  const APInt *CI;
2560  if (Op0KnownZeroInverted == 1 &&
2561  match(LHS, m_LShr(m_Power2(CI), m_Value(X))))
2562  return new ICmpInst(ICmpInst::ICMP_NE, X,
2564  CI->countTrailingZeros()));
2565  }
2566 
2567  break;
2568  }
2569  case ICmpInst::ICMP_NE: {
2570  if (Op0Max.ult(Op1Min) || Op0Min.ugt(Op1Max))
2572 
2573  // If all bits are known zero except for one, then we know at most one
2574  // bit is set. If the comparison is against zero, then this is a check
2575  // to see if *that* bit is set.
2576  APInt Op0KnownZeroInverted = ~Op0KnownZero;
2577  if (~Op1KnownZero == 0) {
2578  // If the LHS is an AND with the same constant, look through it.
2579  Value *LHS = nullptr;
2580  ConstantInt *LHSC = nullptr;
2581  if (!match(Op0, m_And(m_Value(LHS), m_ConstantInt(LHSC))) ||
2582  LHSC->getValue() != Op0KnownZeroInverted)
2583  LHS = Op0;
2584 
2585  // If the LHS is 1 << x, and we know the result is a power of 2 like 8,
2586  // then turn "((1 << x)&8) != 0" into "x == 3".
2587  // or turn "((1 << x)&7) != 0" into "x < 3".
2588  Value *X = nullptr;
2589  if (match(LHS, m_Shl(m_One(), m_Value(X)))) {
2590  APInt ValToCheck = Op0KnownZeroInverted;
2591  if (ValToCheck.isPowerOf2()) {
2592  unsigned CmpVal = ValToCheck.countTrailingZeros();
2593  return new ICmpInst(ICmpInst::ICMP_EQ, X,
2594  ConstantInt::get(X->getType(), CmpVal));
2595  } else if ((++ValToCheck).isPowerOf2()) {
2596  unsigned CmpVal = ValToCheck.countTrailingZeros();
2597  return new ICmpInst(ICmpInst::ICMP_ULT, X,
2598  ConstantInt::get(X->getType(), CmpVal));
2599  }
2600  }
2601 
2602  // If the LHS is 8 >>u x, and we know the result is a power of 2 like 1,
2603  // then turn "((8 >>u x)&1) != 0" into "x == 3".
2604  const APInt *CI;
2605  if (Op0KnownZeroInverted == 1 &&
2606  match(LHS, m_LShr(m_Power2(CI), m_Value(X))))
2607  return new ICmpInst(ICmpInst::ICMP_EQ, X,
2609  CI->countTrailingZeros()));
2610  }
2611 
2612  break;
2613  }
2614  case ICmpInst::ICMP_ULT:
2615  if (Op0Max.ult(Op1Min)) // A <u B -> true if max(A) < min(B)
2617  if (Op0Min.uge(Op1Max)) // A <u B -> false if min(A) >= max(B)
2619  if (Op1Min == Op0Max) // A <u B -> A != B if max(A) == min(B)
2620  return new ICmpInst(ICmpInst::ICMP_NE, Op0, Op1);
2621  if (ConstantInt *CI = dyn_cast<ConstantInt>(Op1)) {
2622  if (Op1Max == Op0Min+1) // A <u C -> A == C-1 if min(A)+1 == C
2623  return new ICmpInst(ICmpInst::ICMP_EQ, Op0,
2624  Builder->getInt(CI->getValue()-1));
2625 
2626  // (x <u 2147483648) -> (x >s -1) -> true if sign bit clear
2627  if (CI->isMinValue(true))
2628  return new ICmpInst(ICmpInst::ICMP_SGT, Op0,
2630  }
2631  break;
2632  case ICmpInst::ICMP_UGT:
2633  if (Op0Min.ugt(Op1Max)) // A >u B -> true if min(A) > max(B)
2635  if (Op0Max.ule(Op1Min)) // A >u B -> false if max(A) <= max(B)
2637 
2638  if (Op1Max == Op0Min) // A >u B -> A != B if min(A) == max(B)
2639  return new ICmpInst(ICmpInst::ICMP_NE, Op0, Op1);
2640  if (ConstantInt *CI = dyn_cast<ConstantInt>(Op1)) {
2641  if (Op1Min == Op0Max-1) // A >u C -> A == C+1 if max(a)-1 == C
2642  return new ICmpInst(ICmpInst::ICMP_EQ, Op0,
2643  Builder->getInt(CI->getValue()+1));
2644 
2645  // (x >u 2147483647) -> (x <s 0) -> true if sign bit set
2646  if (CI->isMaxValue(true))
2647  return new ICmpInst(ICmpInst::ICMP_SLT, Op0,
2649  }
2650  break;
2651  case ICmpInst::ICMP_SLT:
2652  if (Op0Max.slt(Op1Min)) // A <s B -> true if max(A) < min(C)
2654  if (Op0Min.sge(Op1Max)) // A <s B -> false if min(A) >= max(C)
2656  if (Op1Min == Op0Max) // A <s B -> A != B if max(A) == min(B)
2657  return new ICmpInst(ICmpInst::ICMP_NE, Op0, Op1);
2658  if (ConstantInt *CI = dyn_cast<ConstantInt>(Op1)) {
2659  if (Op1Max == Op0Min+1) // A <s C -> A == C-1 if min(A)+1 == C
2660  return new ICmpInst(ICmpInst::ICMP_EQ, Op0,
2661  Builder->getInt(CI->getValue()-1));
2662  }
2663  break;
2664  case ICmpInst::ICMP_SGT:
2665  if (Op0Min.sgt(Op1Max)) // A >s B -> true if min(A) > max(B)
2667  if (Op0Max.sle(Op1Min)) // A >s B -> false if max(A) <= min(B)
2669 
2670  if (Op1Max == Op0Min) // A >s B -> A != B if min(A) == max(B)
2671  return new ICmpInst(ICmpInst::ICMP_NE, Op0, Op1);
2672  if (ConstantInt *CI = dyn_cast<ConstantInt>(Op1)) {
2673  if (Op1Min == Op0Max-1) // A >s C -> A == C+1 if max(A)-1 == C
2674  return new ICmpInst(ICmpInst::ICMP_EQ, Op0,
2675  Builder->getInt(CI->getValue()+1));
2676  }
2677  break;
2678  case ICmpInst::ICMP_SGE:
2679  assert(!isa<ConstantInt>(Op1) && "ICMP_SGE with ConstantInt not folded!");
2680  if (Op0Min.sge(Op1Max)) // A >=s B -> true if min(A) >= max(B)
2682  if (Op0Max.slt(Op1Min)) // A >=s B -> false if max(A) < min(B)
2684  break;
2685  case ICmpInst::ICMP_SLE:
2686  assert(!isa<ConstantInt>(Op1) && "ICMP_SLE with ConstantInt not folded!");
2687  if (Op0Max.sle(Op1Min)) // A <=s B -> true if max(A) <= min(B)
2689  if (Op0Min.sgt(Op1Max)) // A <=s B -> false if min(A) > max(B)
2691  break;
2692  case ICmpInst::ICMP_UGE:
2693  assert(!isa<ConstantInt>(Op1) && "ICMP_UGE with ConstantInt not folded!");
2694  if (Op0Min.uge(Op1Max)) // A >=u B -> true if min(A) >= max(B)
2696  if (Op0Max.ult(Op1Min)) // A >=u B -> false if max(A) < min(B)
2698  break;
2699  case ICmpInst::ICMP_ULE:
2700  assert(!isa<ConstantInt>(Op1) && "ICMP_ULE with ConstantInt not folded!");
2701  if (Op0Max.ule(Op1Min)) // A <=u B -> true if max(A) <= min(B)
2703  if (Op0Min.ugt(Op1Max)) // A <=u B -> false if min(A) > max(B)
2705  break;
2706  }
2707 
2708  // Turn a signed comparison into an unsigned one if both operands
2709  // are known to have the same sign.
2710  if (I.isSigned() &&
2711  ((Op0KnownZero.isNegative() && Op1KnownZero.isNegative()) ||
2712  (Op0KnownOne.isNegative() && Op1KnownOne.isNegative())))
2713  return new ICmpInst(I.getUnsignedPredicate(), Op0, Op1);
2714  }
2715 
2716  // Test if the ICmpInst instruction is used exclusively by a select as
2717  // part of a minimum or maximum operation. If so, refrain from doing
2718  // any other folding. This helps out other analyses which understand
2719  // non-obfuscated minimum and maximum idioms, such as ScalarEvolution
2720  // and CodeGen. And in this case, at least one of the comparison
2721  // operands has at least one user besides the compare (the select),
2722  // which would often largely negate the benefit of folding anyway.
2723  if (I.hasOneUse())
2724  if (SelectInst *SI = dyn_cast<SelectInst>(*I.user_begin()))
2725  if ((SI->getOperand(1) == Op0 && SI->getOperand(2) == Op1) ||
2726  (SI->getOperand(2) == Op0 && SI->getOperand(1) == Op1))
2727  return nullptr;
2728 
2729  // See if we are doing a comparison between a constant and an instruction that
2730  // can be folded into the comparison.
2731  if (ConstantInt *CI = dyn_cast<ConstantInt>(Op1)) {
2732  // Since the RHS is a ConstantInt (CI), if the left hand side is an
2733  // instruction, see if that instruction also has constants so that the
2734  // instruction can be folded into the icmp
2735  if (Instruction *LHSI = dyn_cast<Instruction>(Op0))
2736  if (Instruction *Res = visitICmpInstWithInstAndIntCst(I, LHSI, CI))
2737  return Res;
2738  }
2739 
2740  // Handle icmp with constant (but not simple integer constant) RHS
2741  if (Constant *RHSC = dyn_cast<Constant>(Op1)) {
2742  if (Instruction *LHSI = dyn_cast<Instruction>(Op0))
2743  switch (LHSI->getOpcode()) {
2744  case Instruction::GetElementPtr:
2745  // icmp pred GEP (P, int 0, int 0, int 0), null -> icmp pred P, null
2746  if (RHSC->isNullValue() &&
2747  cast<GetElementPtrInst>(LHSI)->hasAllZeroIndices())
2748  return new ICmpInst(I.getPredicate(), LHSI->getOperand(0),
2749  Constant::getNullValue(LHSI->getOperand(0)->getType()));
2750  break;
2751  case Instruction::PHI:
2752  // Only fold icmp into the PHI if the phi and icmp are in the same
2753  // block. If in the same block, we're encouraging jump threading. If
2754  // not, we are just pessimizing the code by making an i1 phi.
2755  if (LHSI->getParent() == I.getParent())
2756  if (Instruction *NV = FoldOpIntoPhi(I))
2757  return NV;
2758  break;
2759  case Instruction::Select: {
2760  // If either operand of the select is a constant, we can fold the
2761  // comparison into the select arms, which will cause one to be
2762  // constant folded and the select turned into a bitwise or.
2763  Value *Op1 = nullptr, *Op2 = nullptr;
2764  if (Constant *C = dyn_cast<Constant>(LHSI->getOperand(1)))
2765  Op1 = ConstantExpr::getICmp(I.getPredicate(), C, RHSC);
2766  if (Constant *C = dyn_cast<Constant>(LHSI->getOperand(2)))
2767  Op2 = ConstantExpr::getICmp(I.getPredicate(), C, RHSC);
2768 
2769  // We only want to perform this transformation if it will not lead to
2770  // additional code. This is true if either both sides of the select
2771  // fold to a constant (in which case the icmp is replaced with a select
2772  // which will usually simplify) or this is the only user of the
2773  // select (in which case we are trading a select+icmp for a simpler
2774  // select+icmp).
2775  if ((Op1 && Op2) || (LHSI->hasOneUse() && (Op1 || Op2))) {
2776  if (!Op1)
2777  Op1 = Builder->CreateICmp(I.getPredicate(), LHSI->getOperand(1),
2778  RHSC, I.getName());
2779  if (!Op2)
2780  Op2 = Builder->CreateICmp(I.getPredicate(), LHSI->getOperand(2),
2781  RHSC, I.getName());
2782  return SelectInst::Create(LHSI->getOperand(0), Op1, Op2);
2783  }
2784  break;
2785  }
2786  case Instruction::IntToPtr:
2787  // icmp pred inttoptr(X), null -> icmp pred X, 0
2788  if (RHSC->isNullValue() && DL &&
2789  DL->getIntPtrType(RHSC->getType()) ==
2790  LHSI->getOperand(0)->getType())
2791  return new ICmpInst(I.getPredicate(), LHSI->getOperand(0),
2792  Constant::getNullValue(LHSI->getOperand(0)->getType()));
2793  break;
2794 
2795  case Instruction::Load:
2796  // Try to optimize things like "A[i] > 4" to index computations.
2797  if (GetElementPtrInst *GEP =
2798  dyn_cast<GetElementPtrInst>(LHSI->getOperand(0))) {
2799  if (GlobalVariable *GV = dyn_cast<GlobalVariable>(GEP->getOperand(0)))
2800  if (GV->isConstant() && GV->hasDefinitiveInitializer() &&
2801  !cast<LoadInst>(LHSI)->isVolatile())
2802  if (Instruction *Res = FoldCmpLoadFromIndexedGlobal(GEP, GV, I))
2803  return Res;
2804  }
2805  break;
2806  }
2807  }
2808 
2809  // If we can optimize a 'icmp GEP, P' or 'icmp P, GEP', do so now.
2810  if (GEPOperator *GEP = dyn_cast<GEPOperator>(Op0))
2811  if (Instruction *NI = FoldGEPICmp(GEP, Op1, I.getPredicate(), I))
2812  return NI;
2813  if (GEPOperator *GEP = dyn_cast<GEPOperator>(Op1))
2814  if (Instruction *NI = FoldGEPICmp(GEP, Op0,
2816  return NI;
2817 
2818  // Test to see if the operands of the icmp are casted versions of other
2819  // values. If the ptr->ptr cast can be stripped off both arguments, we do so
2820  // now.
2821  if (BitCastInst *CI = dyn_cast<BitCastInst>(Op0)) {
2822  if (Op0->getType()->isPointerTy() &&
2823  (isa<Constant>(Op1) || isa<BitCastInst>(Op1))) {
2824  // We keep moving the cast from the left operand over to the right
2825  // operand, where it can often be eliminated completely.
2826  Op0 = CI->getOperand(0);
2827 
2828  // If operand #1 is a bitcast instruction, it must also be a ptr->ptr cast
2829  // so eliminate it as well.
2830  if (BitCastInst *CI2 = dyn_cast<BitCastInst>(Op1))
2831  Op1 = CI2->getOperand(0);
2832 
2833  // If Op1 is a constant, we can fold the cast into the constant.
2834  if (Op0->getType() != Op1->getType()) {
2835  if (Constant *Op1C = dyn_cast<Constant>(Op1)) {
2836  Op1 = ConstantExpr::getBitCast(Op1C, Op0->getType());
2837  } else {
2838  // Otherwise, cast the RHS right before the icmp
2839  Op1 = Builder->CreateBitCast(Op1, Op0->getType());
2840  }
2841  }
2842  return new ICmpInst(I.getPredicate(), Op0, Op1);
2843  }
2844  }
2845 
2846  if (isa<CastInst>(Op0)) {
2847  // Handle the special case of: icmp (cast bool to X), <cst>
2848  // This comes up when you have code like
2849  // int X = A < B;
2850  // if (X) ...
2851  // For generality, we handle any zero-extension of any operand comparison
2852  // with a constant or another cast from the same type.
2853  if (isa<Constant>(Op1) || isa<CastInst>(Op1))
2855  return R;
2856  }
2857 
2858  // Special logic for binary operators.
2859  BinaryOperator *BO0 = dyn_cast<BinaryOperator>(Op0);
2860  BinaryOperator *BO1 = dyn_cast<BinaryOperator>(Op1);
2861  if (BO0 || BO1) {
2862  CmpInst::Predicate Pred = I.getPredicate();
2863  bool NoOp0WrapProblem = false, NoOp1WrapProblem = false;
2864  if (BO0 && isa<OverflowingBinaryOperator>(BO0))
2865  NoOp0WrapProblem = ICmpInst::isEquality(Pred) ||
2866  (CmpInst::isUnsigned(Pred) && BO0->hasNoUnsignedWrap()) ||
2867  (CmpInst::isSigned(Pred) && BO0->hasNoSignedWrap());
2868  if (BO1 && isa<OverflowingBinaryOperator>(BO1))
2869  NoOp1WrapProblem = ICmpInst::isEquality(Pred) ||
2870  (CmpInst::isUnsigned(Pred) && BO1->hasNoUnsignedWrap()) ||
2871  (CmpInst::isSigned(Pred) && BO1->hasNoSignedWrap());
2872 
2873  // Analyze the case when either Op0 or Op1 is an add instruction.
2874  // Op0 = A + B (or A and B are null); Op1 = C + D (or C and D are null).
2875  Value *A = nullptr, *B = nullptr, *C = nullptr, *D = nullptr;
2876  if (BO0 && BO0->getOpcode() == Instruction::Add)
2877  A = BO0->getOperand(0), B = BO0->getOperand(1);
2878  if (BO1 && BO1->getOpcode() == Instruction::Add)
2879  C = BO1->getOperand(0), D = BO1->getOperand(1);
2880 
2881  // icmp (X+Y), X -> icmp Y, 0 for equalities or if there is no overflow.
2882  if ((A == Op1 || B == Op1) && NoOp0WrapProblem)
2883  return new ICmpInst(Pred, A == Op1 ? B : A,
2885 
2886  // icmp X, (X+Y) -> icmp 0, Y for equalities or if there is no overflow.
2887  if ((C == Op0 || D == Op0) && NoOp1WrapProblem)
2888  return new ICmpInst(Pred, Constant::getNullValue(Op0->getType()),
2889  C == Op0 ? D : C);
2890 
2891  // icmp (X+Y), (X+Z) -> icmp Y, Z for equalities or if there is no overflow.
2892  if (A && C && (A == C || A == D || B == C || B == D) &&
2893  NoOp0WrapProblem && NoOp1WrapProblem &&
2894  // Try not to increase register pressure.
2895  BO0->hasOneUse() && BO1->hasOneUse()) {
2896  // Determine Y and Z in the form icmp (X+Y), (X+Z).
2897  Value *Y, *Z;
2898  if (A == C) {
2899  // C + B == C + D -> B == D
2900  Y = B;
2901  Z = D;
2902  } else if (A == D) {
2903  // D + B == C + D -> B == C
2904  Y = B;
2905  Z = C;
2906  } else if (B == C) {
2907  // A + C == C + D -> A == D
2908  Y = A;
2909  Z = D;
2910  } else {
2911  assert(B == D);
2912  // A + D == C + D -> A == C
2913  Y = A;
2914  Z = C;
2915  }
2916  return new ICmpInst(Pred, Y, Z);
2917  }
2918 
2919  // icmp slt (X + -1), Y -> icmp sle X, Y
2920  if (A && NoOp0WrapProblem && Pred == CmpInst::ICMP_SLT &&
2921  match(B, m_AllOnes()))
2922  return new ICmpInst(CmpInst::ICMP_SLE, A, Op1);
2923 
2924  // icmp sge (X + -1), Y -> icmp sgt X, Y
2925  if (A && NoOp0WrapProblem && Pred == CmpInst::ICMP_SGE &&
2926  match(B, m_AllOnes()))
2927  return new ICmpInst(CmpInst::ICMP_SGT, A, Op1);
2928 
2929  // icmp sle (X + 1), Y -> icmp slt X, Y
2930  if (A && NoOp0WrapProblem && Pred == CmpInst::ICMP_SLE &&
2931  match(B, m_One()))
2932  return new ICmpInst(CmpInst::ICMP_SLT, A, Op1);
2933 
2934  // icmp sgt (X + 1), Y -> icmp sge X, Y
2935  if (A && NoOp0WrapProblem && Pred == CmpInst::ICMP_SGT &&
2936  match(B, m_One()))
2937  return new ICmpInst(CmpInst::ICMP_SGE, A, Op1);
2938 
2939  // if C1 has greater magnitude than C2:
2940  // icmp (X + C1), (Y + C2) -> icmp (X + C3), Y
2941  // s.t. C3 = C1 - C2
2942  //
2943  // if C2 has greater magnitude than C1:
2944  // icmp (X + C1), (Y + C2) -> icmp X, (Y + C3)
2945  // s.t. C3 = C2 - C1
2946  if (A && C && NoOp0WrapProblem && NoOp1WrapProblem &&
2947  (BO0->hasOneUse() || BO1->hasOneUse()) && !I.isUnsigned())
2948  if (ConstantInt *C1 = dyn_cast<ConstantInt>(B))
2949  if (ConstantInt *C2 = dyn_cast<ConstantInt>(D)) {
2950  const APInt &AP1 = C1->getValue();
2951  const APInt &AP2 = C2->getValue();
2952  if (AP1.isNegative() == AP2.isNegative()) {
2953  APInt AP1Abs = C1->getValue().abs();
2954  APInt AP2Abs = C2->getValue().abs();
2955  if (AP1Abs.uge(AP2Abs)) {
2956  ConstantInt *C3 = Builder->getInt(AP1 - AP2);
2957  Value *NewAdd = Builder->CreateNSWAdd(A, C3);
2958  return new ICmpInst(Pred, NewAdd, C);
2959  } else {
2960  ConstantInt *C3 = Builder->getInt(AP2 - AP1);
2961  Value *NewAdd = Builder->CreateNSWAdd(C, C3);
2962  return new ICmpInst(Pred, A, NewAdd);
2963  }
2964  }
2965  }
2966 
2967 
2968  // Analyze the case when either Op0 or Op1 is a sub instruction.
2969  // Op0 = A - B (or A and B are null); Op1 = C - D (or C and D are null).
2970  A = nullptr; B = nullptr; C = nullptr; D = nullptr;
2971  if (BO0 && BO0->getOpcode() == Instruction::Sub)
2972  A = BO0->getOperand(0), B = BO0->getOperand(1);
2973  if (BO1 && BO1->getOpcode() == Instruction::Sub)
2974  C = BO1->getOperand(0), D = BO1->getOperand(1);
2975 
2976  // icmp (X-Y), X -> icmp 0, Y for equalities or if there is no overflow.
2977  if (A == Op1 && NoOp0WrapProblem)
2978  return new ICmpInst(Pred, Constant::getNullValue(Op1->getType()), B);
2979 
2980  // icmp X, (X-Y) -> icmp Y, 0 for equalities or if there is no overflow.
2981  if (C == Op0 && NoOp1WrapProblem)
2982  return new ICmpInst(Pred, D, Constant::getNullValue(Op0->getType()));
2983 
2984  // icmp (Y-X), (Z-X) -> icmp Y, Z for equalities or if there is no overflow.
2985  if (B && D && B == D && NoOp0WrapProblem && NoOp1WrapProblem &&
2986  // Try not to increase register pressure.
2987  BO0->hasOneUse() && BO1->hasOneUse())
2988  return new ICmpInst(Pred, A, C);
2989 
2990  // icmp (X-Y), (X-Z) -> icmp Z, Y for equalities or if there is no overflow.
2991  if (A && C && A == C && NoOp0WrapProblem && NoOp1WrapProblem &&
2992  // Try not to increase register pressure.
2993  BO0->hasOneUse() && BO1->hasOneUse())
2994  return new ICmpInst(Pred, D, B);
2995 
2996  // icmp (0-X) < cst --> x > -cst
2997  if (NoOp0WrapProblem && ICmpInst::isSigned(Pred)) {
2998  Value *X;
2999  if (match(BO0, m_Neg(m_Value(X))))
3000  if (ConstantInt *RHSC = dyn_cast<ConstantInt>(Op1))
3001  if (!RHSC->isMinValue(/*isSigned=*/true))
3002  return new ICmpInst(I.getSwappedPredicate(), X,
3003  ConstantExpr::getNeg(RHSC));
3004  }
3005 
3006  BinaryOperator *SRem = nullptr;
3007  // icmp (srem X, Y), Y
3008  if (BO0 && BO0->getOpcode() == Instruction::SRem &&
3009  Op1 == BO0->getOperand(1))
3010  SRem = BO0;
3011  // icmp Y, (srem X, Y)
3012  else if (BO1 && BO1->getOpcode() == Instruction::SRem &&
3013  Op0 == BO1->getOperand(1))
3014  SRem = BO1;
3015  if (SRem) {
3016  // We don't check hasOneUse to avoid increasing register pressure because
3017  // the value we use is the same value this instruction was already using.
3018  switch (SRem == BO0 ? ICmpInst::getSwappedPredicate(Pred) : Pred) {
3019  default: break;
3020  case ICmpInst::ICMP_EQ:
3022  case ICmpInst::ICMP_NE:
3024  case ICmpInst::ICMP_SGT:
3025  case ICmpInst::ICMP_SGE:
3026  return new ICmpInst(ICmpInst::ICMP_SGT, SRem->getOperand(1),
3028  case ICmpInst::ICMP_SLT:
3029  case ICmpInst::ICMP_SLE:
3030  return new ICmpInst(ICmpInst::ICMP_SLT, SRem->getOperand(1),
3031  Constant::getNullValue(SRem->getType()));
3032  }
3033  }
3034 
3035  if (BO0 && BO1 && BO0->getOpcode() == BO1->getOpcode() &&
3036  BO0->hasOneUse() && BO1->hasOneUse() &&
3037  BO0->getOperand(1) == BO1->getOperand(1)) {
3038  switch (BO0->getOpcode()) {
3039  default: break;
3040  case Instruction::Add:
3041  case Instruction::Sub:
3042  case Instruction::Xor:
3043  if (I.isEquality()) // a+x icmp eq/ne b+x --> a icmp b
3044  return new ICmpInst(I.getPredicate(), BO0->getOperand(0),
3045  BO1->getOperand(0));
3046  // icmp u/s (a ^ signbit), (b ^ signbit) --> icmp s/u a, b
3047  if (ConstantInt *CI = dyn_cast<ConstantInt>(BO0->getOperand(1))) {
3048  if (CI->getValue().isSignBit()) {
3049  ICmpInst::Predicate Pred = I.isSigned()
3050  ? I.getUnsignedPredicate()
3051  : I.getSignedPredicate();
3052  return new ICmpInst(Pred, BO0->getOperand(0),
3053  BO1->getOperand(0));
3054  }
3055 
3056  if (CI->isMaxValue(true)) {
3057  ICmpInst::Predicate Pred = I.isSigned()
3058  ? I.getUnsignedPredicate()
3059  : I.getSignedPredicate();
3060  Pred = I.getSwappedPredicate(Pred);
3061  return new ICmpInst(Pred, BO0->getOperand(0),
3062  BO1->getOperand(0));
3063  }
3064  }
3065  break;
3066  case Instruction::Mul:
3067  if (!I.isEquality())
3068  break;
3069 
3070  if (ConstantInt *CI = dyn_cast<ConstantInt>(BO0->getOperand(1))) {
3071  // a * Cst icmp eq/ne b * Cst --> a & Mask icmp b & Mask
3072  // Mask = -1 >> count-trailing-zeros(Cst).
3073  if (!CI->isZero() && !CI->isOne()) {
3074  const APInt &AP = CI->getValue();
3077  AP.getBitWidth() -
3078  AP.countTrailingZeros()));
3079  Value *And1 = Builder->CreateAnd(BO0->getOperand(0), Mask);
3080  Value *And2 = Builder->CreateAnd(BO1->getOperand(0), Mask);
3081  return new ICmpInst(I.getPredicate(), And1, And2);
3082  }
3083  }
3084  break;
3085  case Instruction::UDiv:
3086  case Instruction::LShr:
3087  if (I.isSigned())
3088  break;
3089  // fall-through
3090  case Instruction::SDiv:
3091  case Instruction::AShr:
3092  if (!BO0->isExact() || !BO1->isExact())
3093  break;
3094  return new ICmpInst(I.getPredicate(), BO0->getOperand(0),
3095  BO1->getOperand(0));
3096  case Instruction::Shl: {
3097  bool NUW = BO0->hasNoUnsignedWrap() && BO1->hasNoUnsignedWrap();
3098  bool NSW = BO0->hasNoSignedWrap() && BO1->hasNoSignedWrap();
3099  if (!NUW && !NSW)
3100  break;
3101  if (!NSW && I.isSigned())
3102  break;
3103  return new ICmpInst(I.getPredicate(), BO0->getOperand(0),
3104  BO1->getOperand(0));
3105  }
3106  }
3107  }
3108  }
3109 
3110  { Value *A, *B;
3111  // Transform (A & ~B) == 0 --> (A & B) != 0
3112  // and (A & ~B) != 0 --> (A & B) == 0
3113  // if A is a power of 2.
3114  if (match(Op0, m_And(m_Value(A), m_Not(m_Value(B)))) &&
3115  match(Op1, m_Zero()) && isKnownToBeAPowerOfTwo(A) && I.isEquality())
3116  return new ICmpInst(I.getInversePredicate(),
3117  Builder->CreateAnd(A, B),
3118  Op1);
3119 
3120  // ~x < ~y --> y < x
3121  // ~x < cst --> ~cst < x
3122  if (match(Op0, m_Not(m_Value(A)))) {
3123  if (match(Op1, m_Not(m_Value(B))))
3124  return new ICmpInst(I.getPredicate(), B, A);
3125  if (ConstantInt *RHSC = dyn_cast<ConstantInt>(Op1))
3126  return new ICmpInst(I.getPredicate(), ConstantExpr::getNot(RHSC), A);
3127  }
3128 
3129  // (a+b) <u a --> llvm.uadd.with.overflow.
3130  // (a+b) <u b --> llvm.uadd.with.overflow.
3131  if (I.getPredicate() == ICmpInst::ICMP_ULT &&
3132  match(Op0, m_Add(m_Value(A), m_Value(B))) &&
3133  (Op1 == A || Op1 == B))
3134  if (Instruction *R = ProcessUAddIdiom(I, Op0, *this))
3135  return R;
3136 
3137  // a >u (a+b) --> llvm.uadd.with.overflow.
3138  // b >u (a+b) --> llvm.uadd.with.overflow.
3139  if (I.getPredicate() == ICmpInst::ICMP_UGT &&
3140  match(Op1, m_Add(m_Value(A), m_Value(B))) &&
3141  (Op0 == A || Op0 == B))
3142  if (Instruction *R = ProcessUAddIdiom(I, Op1, *this))
3143  return R;
3144 
3145  // (zext a) * (zext b) --> llvm.umul.with.overflow.
3146  if (match(Op0, m_Mul(m_ZExt(m_Value(A)), m_ZExt(m_Value(B))))) {
3147  if (Instruction *R = ProcessUMulZExtIdiom(I, Op0, Op1, *this))
3148  return R;
3149  }
3150  if (match(Op1, m_Mul(m_ZExt(m_Value(A)), m_ZExt(m_Value(B))))) {
3151  if (Instruction *R = ProcessUMulZExtIdiom(I, Op1, Op0, *this))
3152  return R;
3153  }
3154  }
3155 
3156  if (I.isEquality()) {
3157  Value *A, *B, *C, *D;
3158 
3159  if (match(Op0, m_Xor(m_Value(A), m_Value(B)))) {
3160  if (A == Op1 || B == Op1) { // (A^B) == A -> B == 0
3161  Value *OtherVal = A == Op1 ? B : A;
3162  return new ICmpInst(I.getPredicate(), OtherVal,
3164  }
3165 
3166  if (match(Op1, m_Xor(m_Value(C), m_Value(D)))) {
3167  // A^c1 == C^c2 --> A == C^(c1^c2)
3168  ConstantInt *C1, *C2;
3169  if (match(B, m_ConstantInt(C1)) &&
3170  match(D, m_ConstantInt(C2)) && Op1->hasOneUse()) {
3171  Constant *NC = Builder->getInt(C1->getValue() ^ C2->getValue());
3172  Value *Xor = Builder->CreateXor(C, NC);
3173  return new ICmpInst(I.getPredicate(), A, Xor);
3174  }
3175 
3176  // A^B == A^D -> B == D
3177  if (A == C) return new ICmpInst(I.getPredicate(), B, D);
3178  if (A == D) return new ICmpInst(I.getPredicate(), B, C);
3179  if (B == C) return new ICmpInst(I.getPredicate(), A, D);
3180  if (B == D) return new ICmpInst(I.getPredicate(), A, C);
3181  }
3182  }
3183 
3184  if (match(Op1, m_Xor(m_Value(A), m_Value(B))) &&
3185  (A == Op0 || B == Op0)) {
3186  // A == (A^B) -> B == 0
3187  Value *OtherVal = A == Op0 ? B : A;
3188  return new ICmpInst(I.getPredicate(), OtherVal,
3190  }
3191 
3192  // (X&Z) == (Y&Z) -> (X^Y) & Z == 0
3193  if (match(Op0, m_OneUse(m_And(m_Value(A), m_Value(B)))) &&
3194  match(Op1, m_OneUse(m_And(m_Value(C), m_Value(D))))) {
3195  Value *X = nullptr, *Y = nullptr, *Z = nullptr;
3196 
3197  if (A == C) {
3198  X = B; Y = D; Z = A;
3199  } else if (A == D) {
3200  X = B; Y = C; Z = A;
3201  } else if (B == C) {
3202  X = A; Y = D; Z = B;
3203  } else if (B == D) {
3204  X = A; Y = C; Z = B;
3205  }
3206 
3207  if (X) { // Build (X^Y) & Z
3208  Op1 = Builder->CreateXor(X, Y);
3209  Op1 = Builder->CreateAnd(Op1, Z);
3210  I.setOperand(0, Op1);
3211  I.setOperand(1, Constant::getNullValue(Op1->getType()));
3212  return &I;
3213  }
3214  }
3215 
3216  // Transform (zext A) == (B & (1<<X)-1) --> A == (trunc B)
3217  // and (B & (1<<X)-1) == (zext A) --> A == (trunc B)
3218  ConstantInt *Cst1;
3219  if ((Op0->hasOneUse() &&
3220  match(Op0, m_ZExt(m_Value(A))) &&
3221  match(Op1, m_And(m_Value(B), m_ConstantInt(Cst1)))) ||
3222  (Op1->hasOneUse() &&
3223  match(Op0, m_And(m_Value(B), m_ConstantInt(Cst1))) &&
3224  match(Op1, m_ZExt(m_Value(A))))) {
3225  APInt Pow2 = Cst1->getValue() + 1;
3226  if (Pow2.isPowerOf2() && isa<IntegerType>(A->getType()) &&
3227  Pow2.logBase2() == cast<IntegerType>(A->getType())->getBitWidth())
3228  return new ICmpInst(I.getPredicate(), A,
3229  Builder->CreateTrunc(B, A->getType()));
3230  }
3231 
3232  // (A >> C) == (B >> C) --> (A^B) u< (1 << C)
3233  // For lshr and ashr pairs.
3234  if ((match(Op0, m_OneUse(m_LShr(m_Value(A), m_ConstantInt(Cst1)))) &&
3235  match(Op1, m_OneUse(m_LShr(m_Value(B), m_Specific(Cst1))))) ||
3236  (match(Op0, m_OneUse(m_AShr(m_Value(A), m_ConstantInt(Cst1)))) &&
3237  match(Op1, m_OneUse(m_AShr(m_Value(B), m_Specific(Cst1)))))) {
3238  unsigned TypeBits = Cst1->getBitWidth();
3239  unsigned ShAmt = (unsigned)Cst1->getLimitedValue(TypeBits);
3240  if (ShAmt < TypeBits && ShAmt != 0) {
3244  Value *Xor = Builder->CreateXor(A, B, I.getName() + ".unshifted");
3245  APInt CmpVal = APInt::getOneBitSet(TypeBits, ShAmt);
3246  return new ICmpInst(Pred, Xor, Builder->getInt(CmpVal));
3247  }
3248  }
3249 
3250  // Transform "icmp eq (trunc (lshr(X, cst1)), cst" to
3251  // "icmp (and X, mask), cst"
3252  uint64_t ShAmt = 0;
3253  if (Op0->hasOneUse() &&
3255  m_ConstantInt(ShAmt))))) &&
3256  match(Op1, m_ConstantInt(Cst1)) &&
3257  // Only do this when A has multiple uses. This is most important to do
3258  // when it exposes other optimizations.
3259  !A->hasOneUse()) {
3260  unsigned ASize =cast<IntegerType>(A->getType())->getPrimitiveSizeInBits();
3261 
3262  if (ShAmt < ASize) {
3263  APInt MaskV =
3264  APInt::getLowBitsSet(ASize, Op0->getType()->getPrimitiveSizeInBits());
3265  MaskV <<= ShAmt;
3266 
3267  APInt CmpV = Cst1->getValue().zext(ASize);
3268  CmpV <<= ShAmt;
3269 
3270  Value *Mask = Builder->CreateAnd(A, Builder->getInt(MaskV));
3271  return new ICmpInst(I.getPredicate(), Mask, Builder->getInt(CmpV));
3272  }
3273  }
3274  }
3275 
3276  {
3277  Value *X; ConstantInt *Cst;
3278  // icmp X+Cst, X
3279  if (match(Op0, m_Add(m_Value(X), m_ConstantInt(Cst))) && Op1 == X)
3280  return FoldICmpAddOpCst(I, X, Cst, I.getPredicate());
3281 
3282  // icmp X, X+Cst
3283  if (match(Op1, m_Add(m_Value(X), m_ConstantInt(Cst))) && Op0 == X)
3284  return FoldICmpAddOpCst(I, X, Cst, I.getSwappedPredicate());
3285  }
3286  return Changed ? &I : nullptr;
3287 }
Instruction * FoldOpIntoPhi(Instruction &I)
const Use & getOperandUse(unsigned i) const
Definition: User.h:101
BinaryOp_match< LHS, RHS, Instruction::And > m_And(const LHS &L, const RHS &R)
Definition: PatternMatch.h:467
static ConstantInt * getFalse(LLVMContext &Context)
Definition: Constants.cpp:481
class_match< Value > m_Value()
m_Value() - Match an arbitrary value and ignore it.
Definition: PatternMatch.h:70
APInt LLVM_ATTRIBUTE_UNUSED_RESULT abs() const
Get the absolute value;.
Definition: APInt.h:1546
static APInt getAllOnesValue(unsigned numBits)
Get the all-ones value.
Definition: APInt.h:448
BinaryOp_match< LHS, RHS, Instruction::Sub > m_Sub(const LHS &L, const RHS &R)
Definition: PatternMatch.h:407
static BinaryOperator * CreateNot(Value *Op, const Twine &Name="", Instruction *InsertBefore=nullptr)
Instruction * FoldGEPICmp(GEPOperator *GEPLHS, Value *RHS, ICmpInst::Predicate Cond, Instruction &I)
Value * CreateICmp(CmpInst::Predicate P, Value *LHS, Value *RHS, const Twine &Name="")
Definition: IRBuilder.h:1321
static void ComputeSignedMinMaxValuesFromKnownBits(const APInt &KnownZero, const APInt &KnownOne, APInt &Min, APInt &Max)
match_zero m_Zero()
Definition: PatternMatch.h:137
unsigned getBitWidth() const
getBitWidth - Return the bitwidth of this constant.
Definition: Constants.h:110
bool SimplifyDemandedBits(Use &U, APInt DemandedMask, APInt &KnownZero, APInt &KnownOne, unsigned Depth=0)
BinaryOp_match< LHS, RHS, Instruction::Mul > m_Mul(const LHS &L, const RHS &R)
Definition: PatternMatch.h:419
static APInt getLowBitsSet(unsigned numBits, unsigned loBitsSet)
Get a value with low bits set.
Definition: APInt.h:526
Predicate getInversePredicate() const
Return the inverse of the instruction's predicate.
Definition: InstrTypes.h:758
unsigned less or equal
Definition: InstrTypes.h:698
unsigned less than
Definition: InstrTypes.h:697
BinaryOp_match< LHS, RHS, Instruction::AShr > m_AShr(const LHS &L, const RHS &R)
Definition: PatternMatch.h:497
bool isSigned() const
Determine if this instruction is using a signed comparison.
Definition: InstrTypes.h:801
static RegisterPass< NVPTXAllocaHoisting > X("alloca-hoisting","Hoisting alloca instructions in non-entry ""blocks to the entry block")
static bool isEquality(Predicate P)
static Constant * getNullValue(Type *Ty)
Definition: Constants.cpp:133
StringRef getName() const
Definition: Value.cpp:168
Value * CreateNSWAdd(Value *LHS, Value *RHS, const Twine &Name="")
Definition: IRBuilder.h:640
APInt Not(const APInt &APIVal)
Bitwise complement function.
Definition: APInt.h:1880
bool isNegative() const
Determine sign of this APInt.
Definition: APInt.h:320
bool match(Val *V, const Pattern &P)
Definition: PatternMatch.h:42
Value * CreateXor(Value *LHS, Value *RHS, const Twine &Name="")
Definition: IRBuilder.h:838
static unsigned getComplexity(Value *V)
Definition: InstCombine.h:47
static unsigned getBitWidth(Type *Ty, const DataLayout *TD)
BinaryOp_match< LHS, RHS, Instruction::Xor > m_Xor(const LHS &L, const RHS &R)
Definition: PatternMatch.h:479
const APInt & getValue() const
Return the constant's value.
Definition: Constants.h:105
#define llvm_unreachable(msg)
Definition: ErrorHandling.h:98
static Instruction * ProcessUAddIdiom(Instruction &I, Value *OrigAddV, InstCombiner &IC)
CastClass_match< OpTy, Instruction::Trunc > m_Trunc(const OpTy &Op)
m_Trunc
Definition: PatternMatch.h:768
not_match< LHS > m_Not(const LHS &L)
Definition: PatternMatch.h:828
BinaryOp_match< LHS, RHS, Instruction::Add > m_Add(const LHS &L, const RHS &R)
Definition: PatternMatch.h:395
Value * CreateAnd(Value *LHS, Value *RHS, const Twine &Name="")
Definition: IRBuilder.h:806
static SelectInst * Create(Value *C, Value *S1, Value *S2, const Twine &NameStr="", Instruction *InsertBefore=nullptr)
CastClass_match< OpTy, Instruction::ZExt > m_ZExt(const OpTy &Op)
m_ZExt
Definition: PatternMatch.h:782
static Constant * getICmp(unsigned short pred, Constant *LHS, Constant *RHS)
Definition: Constants.cpp:1943
uint64_t getLimitedValue(uint64_t Limit=~0ULL) const
Get the constant's value with a saturation limit.
Definition: Constants.h:218
This class represents a no-op cast from one type to another.
static APInt DemandedBitsLHSMask(ICmpInst &I, unsigned BitWidth, bool isSignCheck)
class_match< ConstantInt > m_ConstantInt()
m_ConstantInt() - Match an arbitrary ConstantInt and ignore it.
Definition: PatternMatch.h:72
Predicate getUnsignedPredicate() const
Return the unsigned version of the predicate.
SelectClass_match< Cond, LHS, RHS > m_Select(const Cond &C, const LHS &L, const RHS &R)
Definition: PatternMatch.h:720
cst_pred_ty< is_power2 > m_Power2()
m_Power2() - Match an integer or vector power of 2.
Definition: PatternMatch.h:281
Instruction * visitICmpInstWithCastAndCast(ICmpInst &ICI)
Value * dyn_castNegVal(Value *V) const
static Instruction * ProcessUMulZExtIdiom(ICmpInst &I, Value *MulVal, Value *OtherVal, InstCombiner &IC)
Recognize and process idiom involving test for multiplication overflow.
OneUse_match< T > m_OneUse(const T &SubPattern)
Definition: PatternMatch.h:60
bool isIntOrIntVectorTy() const
Definition: Type.h:201
static Instruction * ProcessUGT_ADDCST_ADD(ICmpInst &I, Value *A, Value *B, ConstantInt *CI2, ConstantInt *CI1, InstCombiner &IC)
BinaryOp_match< LHS, RHS, Instruction::LShr > m_LShr(const LHS &L, const RHS &R)
Definition: PatternMatch.h:491
assert(Globals.size() > 1)
static CmpInst * Create(OtherOps Op, unsigned short predicate, Value *S1, Value *S2, const Twine &Name="", Instruction *InsertBefore=nullptr)
Create a CmpInst.
static void ComputeUnsignedMinMaxValuesFromKnownBits(const APInt &KnownZero, const APInt &KnownOne, APInt &Min, APInt &Max)
Value * CreateNot(Value *V, const Twine &Name="")
Definition: IRBuilder.h:885
bool isEquality() const
LLVM Constant Representation.
Definition: Constant.h:41
Instruction * ReplaceInstUsesWith(Instruction &I, Value *V)
Definition: InstCombine.h:282
APInt Xor(const APInt &LHS, const APInt &RHS)
Bitwise XOR function for APInt.
Definition: APInt.h:1875
cst_pred_ty< is_all_ones > m_AllOnes()
m_AllOnes() - Match an integer or vector with all bits set to true.
Definition: PatternMatch.h:265
static APInt getOneBitSet(unsigned numBits, unsigned BitNo)
Return an APInt with exactly one bit set in the result.
Definition: APInt.h:474
specificval_ty m_Specific(const Value *V)
m_Specific - Match if we have a specific specified value.
Definition: PatternMatch.h:323
BinaryOp_match< LHS, RHS, Instruction::Shl > m_Shl(const LHS &L, const RHS &R)
Definition: PatternMatch.h:485
Represent an integer comparison operator.
Definition: Instructions.h:977
unsigned getBitWidth() const
Return the number of bits in the APInt.
Definition: APInt.h:1248
bool uge(const APInt &RHS) const
Unsigned greater or equal comparison.
Definition: APInt.h:1112
Value * SimplifyICmpInst(unsigned Predicate, Value *LHS, Value *RHS, const DataLayout *TD=nullptr, const TargetLibraryInfo *TLI=nullptr, const DominatorTree *DT=nullptr)
bool isMaxValue() const
Determine if this is the largest unsigned value.
Definition: APInt.h:348
Value * getOperand(unsigned i) const
Definition: User.h:90
Predicate getPredicate() const
Return the predicate for this instruction.
Definition: InstrTypes.h:735
static Constant * getNot(Constant *C)
Definition: Constants.cpp:2096
static Constant * getAllOnesValue(Type *Ty)
Get the all ones value.
Definition: Constants.cpp:185
BuilderTy * Builder
Definition: InstCombine.h:102
bool isPointerTy() const
Definition: Type.h:217
void swapOperands()
Swap operands and adjust predicate.
LLVMContext & getContext() const
All values hold a context through their type.
Definition: Value.cpp:615
bool hasNoSignedWrap() const
hasNoSignedWrap - Determine whether the no signed wrap flag is set.
bool isPowerOf2() const
Check if this APInt's value is a power of two greater than zero.
Definition: APInt.h:388
signed greater than
Definition: InstrTypes.h:699
neg_match< LHS > m_Neg(const LHS &L)
m_Neg - Match an integer negate.
Definition: PatternMatch.h:854
unsigned countTrailingZeros() const
Count the number of trailing zero bits.
Definition: APInt.cpp:737
IntegerType * getIntPtrType(LLVMContext &C, unsigned AddressSpace=0) const
Definition: DataLayout.cpp:703
BinaryOps getOpcode() const
Definition: InstrTypes.h:326
Definition: test.h:1
static Constant * getBitCast(Constant *C, Type *Ty)
Definition: Constants.cpp:1721
Class for constant integers.
Definition: Constants.h:51
unsigned getScalarSizeInBits() const LLVM_READONLY
Definition: Type.cpp:135
unsigned logBase2() const
Definition: APInt.h:1496
Value * CreateBitCast(Value *V, Type *DestTy, const Twine &Name="")
Definition: IRBuilder.h:1158
Type * getType() const
Definition: Value.h:215
signed less than
Definition: InstrTypes.h:701
bool isKnownToBeAPowerOfTwo(Value *V, bool OrZero=false, unsigned Depth=0)
Predicate getSwappedPredicate() const
Return the predicate as if the operands were swapped.
Definition: InstrTypes.h:774
static Constant * get(Type *Ty, uint64_t V, bool isSigned=false)
Definition: Constants.cpp:528
#define NC
Definition: regutils.h:42
bool isExact() const
isExact - Determine whether the exact flag is set.
static ConstantInt * getTrue(LLVMContext &Context)
Definition: Constants.cpp:474
void setOperand(unsigned i, Value *Val)
Definition: User.h:94
void swap(llvm::BitVector &LHS, llvm::BitVector &RHS)
Implement std::swap in terms of BitVector swap.
Definition: BitVector.h:590
signed less or equal
Definition: InstrTypes.h:702
Class for arbitrary precision integers.
Definition: APInt.h:75
static RegisterPass< SwOnly > Z("legup-sw-only","Replace accelerated functions with wrappers and produce legup_wrappers")
bool isIntegerTy() const
Definition: Type.h:193
LLVM_ATTRIBUTE_UNUSED_RESULT std::enable_if< !is_simple_type< Y >::value, typename cast_retty< X, const Y >::ret_type >::type dyn_cast(const Y &Val)
Definition: Casting.h:265
bool isMinValue() const
Determine if this is the smallest unsigned value.
Definition: APInt.h:363
const Type * getScalarType() const LLVM_READONLY
Definition: Type.cpp:51
static Constant * getNeg(Constant *C, bool HasNUW=false, bool HasNSW=false)
Definition: Constants.cpp:2083
static bool isSignBitCheck(ICmpInst::Predicate pred, ConstantInt *RHS, bool &TrueIfSigned)
unsigned greater or equal
Definition: InstrTypes.h:696
#define I(x, y, z)
Definition: MD5.cpp:54
bool hasOneUse() const
Definition: Value.h:284
bool isSignBit() const
Check if the APInt's value is returned by getSignBit.
Definition: APInt.h:397
Instruction * visitICmpInstWithInstAndIntCst(ICmpInst &ICI, Instruction *LHS, ConstantInt *RHS)
Instruction * FoldCmpLoadFromIndexedGlobal(GetElementPtrInst *GEP, GlobalVariable *GV, CmpInst &ICI, ConstantInt *AndCst=nullptr)
bool isUnsigned() const
Determine if this instruction is using an unsigned comparison.
Definition: InstrTypes.h:807
const DataLayout * DL
Definition: InstCombine.h:89
user_iterator user_begin()
Definition: Value.h:267
static bool swapMayExposeCSEOpportunities(const Value *Op0, const Value *Op1)
Check if the order of Op0 and Op1 as operand in an ICmpInst should be swapped. The decision is based ...
Value * CreateTrunc(Value *V, Type *DestTy, const Twine &Name="")
Definition: IRBuilder.h:1094
LLVM Value Representation.
Definition: Value.h:69
bool hasNoUnsignedWrap() const
hasNoUnsignedWrap - Determine whether the no unsigned wrap flag is set.
cst_pred_ty< is_one > m_One()
m_One() - Match an integer 1 or a vector with all elements equal to 1.
Definition: PatternMatch.h:257
uint64_t getTypeSizeInBits(Type *Ty) const
Definition: DataLayout.h:522
Predicate getSignedPredicate() const
Return the signed version of the predicate.
unsigned greater than
Definition: InstrTypes.h:695
APInt LLVM_ATTRIBUTE_UNUSED_RESULT zext(unsigned width) const
Zero extend to a new width.
Definition: APInt.cpp:984
static GCMetadataPrinterRegistry::Add< OcamlGCMetadataPrinter > Y("ocaml","ocaml 3.10-compatible collector")
ConstantInt * getInt(const APInt &AI)
Get a constant integer value.
Definition: IRBuilder.h:292
const BasicBlock * getParent() const
Definition: Instruction.h:53
Instruction * FoldICmpAddOpCst(Instruction &ICI, Value *X, ConstantInt *CI, ICmpInst::Predicate Pred)
FoldICmpAddOpCst - Fold "icmp pred (X+CI), X".
signed greater or equal
Definition: InstrTypes.h:700
Instruction * InstCombiner::visitICmpInstWithCastAndCast ( ICmpInst ICI)

visitICmpInstWithCastAndCast - Handle icmp (cast x to y), (cast/cst). We only handle extending casts so far.

Definition at line 1790 of file InstCombineCompares.cpp.

1790  {
1791  const CastInst *LHSCI = cast<CastInst>(ICI.getOperand(0));
1792  Value *LHSCIOp = LHSCI->getOperand(0);
1793  Type *SrcTy = LHSCIOp->getType();
1794  Type *DestTy = LHSCI->getType();
1795  Value *RHSCIOp;
1796 
1797  // Turn icmp (ptrtoint x), (ptrtoint/c) into a compare of the input if the
1798  // integer type is the same size as the pointer type.
1799  if (DL && LHSCI->getOpcode() == Instruction::PtrToInt &&
1800  DL->getPointerTypeSizeInBits(SrcTy) == DestTy->getIntegerBitWidth()) {
1801  Value *RHSOp = nullptr;
1802  if (Constant *RHSC = dyn_cast<Constant>(ICI.getOperand(1))) {
1803  RHSOp = ConstantExpr::getIntToPtr(RHSC, SrcTy);
1804  } else if (PtrToIntInst *RHSC = dyn_cast<PtrToIntInst>(ICI.getOperand(1))) {
1805  RHSOp = RHSC->getOperand(0);
1806  // If the pointer types don't match, insert a bitcast.
1807  if (LHSCIOp->getType() != RHSOp->getType())
1808  RHSOp = Builder->CreateBitCast(RHSOp, LHSCIOp->getType());
1809  }
1810 
1811  if (RHSOp)
1812  return new ICmpInst(ICI.getPredicate(), LHSCIOp, RHSOp);
1813  }
1814 
1815  // The code below only handles extension cast instructions, so far.
1816  // Enforce this.
1817  if (LHSCI->getOpcode() != Instruction::ZExt &&
1818  LHSCI->getOpcode() != Instruction::SExt)
1819  return nullptr;
1820 
1821  bool isSignedExt = LHSCI->getOpcode() == Instruction::SExt;
1822  bool isSignedCmp = ICI.isSigned();
1823 
1824  if (CastInst *CI = dyn_cast<CastInst>(ICI.getOperand(1))) {
1825  // Not an extension from the same type?
1826  RHSCIOp = CI->getOperand(0);
1827  if (RHSCIOp->getType() != LHSCIOp->getType())
1828  return nullptr;
1829 
1830  // If the signedness of the two casts doesn't agree (i.e. one is a sext
1831  // and the other is a zext), then we can't handle this.
1832  if (CI->getOpcode() != LHSCI->getOpcode())
1833  return nullptr;
1834 
1835  // Deal with equality cases early.
1836  if (ICI.isEquality())
1837  return new ICmpInst(ICI.getPredicate(), LHSCIOp, RHSCIOp);
1838 
1839  // A signed comparison of sign extended values simplifies into a
1840  // signed comparison.
1841  if (isSignedCmp && isSignedExt)
1842  return new ICmpInst(ICI.getPredicate(), LHSCIOp, RHSCIOp);
1843 
1844  // The other three cases all fold into an unsigned comparison.
1845  return new ICmpInst(ICI.getUnsignedPredicate(), LHSCIOp, RHSCIOp);
1846  }
1847 
1848  // If we aren't dealing with a constant on the RHS, exit early
1849  ConstantInt *CI = dyn_cast<ConstantInt>(ICI.getOperand(1));
1850  if (!CI)
1851  return nullptr;
1852 
1853  // Compute the constant that would happen if we truncated to SrcTy then
1854  // reextended to DestTy.
1855  Constant *Res1 = ConstantExpr::getTrunc(CI, SrcTy);
1856  Constant *Res2 = ConstantExpr::getCast(LHSCI->getOpcode(),
1857  Res1, DestTy);
1858 
1859  // If the re-extended constant didn't change...
1860  if (Res2 == CI) {
1861  // Deal with equality cases early.
1862  if (ICI.isEquality())
1863  return new ICmpInst(ICI.getPredicate(), LHSCIOp, Res1);
1864 
1865  // A signed comparison of sign extended values simplifies into a
1866  // signed comparison.
1867  if (isSignedExt && isSignedCmp)
1868  return new ICmpInst(ICI.getPredicate(), LHSCIOp, Res1);
1869 
1870  // The other three cases all fold into an unsigned comparison.
1871  return new ICmpInst(ICI.getUnsignedPredicate(), LHSCIOp, Res1);
1872  }
1873 
1874  // The re-extended constant changed so the constant cannot be represented
1875  // in the shorter type. Consequently, we cannot emit a simple comparison.
1876  // All the cases that fold to true or false will have already been handled
1877  // by SimplifyICmpInst, so only deal with the tricky case.
1878 
1879  if (isSignedCmp || !isSignedExt)
1880  return nullptr;
1881 
1882  // Evaluate the comparison for LT (we invert for GT below). LE and GE cases
1883  // should have been folded away previously and not enter in here.
1884 
1885  // We're performing an unsigned comp with a sign extended value.
1886  // This is true if the input is >= 0. [aka >s -1]
1887  Constant *NegOne = Constant::getAllOnesValue(SrcTy);
1888  Value *Result = Builder->CreateICmpSGT(LHSCIOp, NegOne, ICI.getName());
1889 
1890  // Finally, return the value computed.
1891  if (ICI.getPredicate() == ICmpInst::ICMP_ULT)
1892  return ReplaceInstUsesWith(ICI, Result);
1893 
1894  assert(ICI.getPredicate() == ICmpInst::ICMP_UGT && "ICmp should be folded!");
1895  return BinaryOperator::CreateNot(Result);
1896 }
Instruction::CastOps getOpcode() const
Return the opcode of this CastInst.
Definition: InstrTypes.h:624
static BinaryOperator * CreateNot(Value *Op, const Twine &Name="", Instruction *InsertBefore=nullptr)
unsigned getPointerTypeSizeInBits(Type *) const
Definition: DataLayout.cpp:611
unsigned less than
Definition: InstrTypes.h:697
bool isSigned() const
Determine if this instruction is using a signed comparison.
Definition: InstrTypes.h:801
static bool isEquality(Predicate P)
StringRef getName() const
Definition: Value.cpp:168
Base class of casting instructions.
Definition: InstrTypes.h:387
Value * CreateICmpSGT(Value *LHS, Value *RHS, const Twine &Name="")
Definition: IRBuilder.h:1265
This class represents a cast from a pointer to an integer.
Predicate getUnsignedPredicate() const
Return the unsigned version of the predicate.
static Constant * getIntToPtr(Constant *C, Type *Ty)
Definition: Constants.cpp:1709
assert(Globals.size() > 1)
LLVM Constant Representation.
Definition: Constant.h:41
Instruction * ReplaceInstUsesWith(Instruction &I, Value *V)
Definition: InstCombine.h:282
Represent an integer comparison operator.
Definition: Instructions.h:977
Value * getOperand(unsigned i) const
Definition: User.h:90
Predicate getPredicate() const
Return the predicate for this instruction.
Definition: InstrTypes.h:735
static Constant * getAllOnesValue(Type *Ty)
Get the all ones value.
Definition: Constants.cpp:185
BuilderTy * Builder
Definition: InstCombine.h:102
unsigned getIntegerBitWidth() const
Definition: Type.cpp:172
Class for constant integers.
Definition: Constants.h:51
Value * CreateBitCast(Value *V, Type *DestTy, const Twine &Name="")
Definition: IRBuilder.h:1158
Type * getType() const
Definition: Value.h:215
static Constant * getTrunc(Constant *C, Type *Ty)
Definition: Constants.cpp:1587
LLVM_ATTRIBUTE_UNUSED_RESULT std::enable_if< !is_simple_type< Y >::value, typename cast_retty< X, const Y >::ret_type >::type dyn_cast(const Y &Val)
Definition: Casting.h:265
const DataLayout * DL
Definition: InstCombine.h:89
LLVM Value Representation.
Definition: Value.h:69
unsigned greater than
Definition: InstrTypes.h:695
static Constant * getCast(unsigned ops, Constant *C, Type *Ty)
Definition: Constants.cpp:1493
Instruction * InstCombiner::visitICmpInstWithInstAndIntCst ( ICmpInst ICI,
Instruction LHSI,
ConstantInt RHS 
)

visitICmpInstWithInstAndIntCst - Handle "icmp (instr, intcst)".

Definition at line 1050 of file InstCombineCompares.cpp.

1052  {
1053  const APInt &RHSV = RHS->getValue();
1054 
1055  switch (LHSI->getOpcode()) {
1056  case Instruction::Trunc:
1057  if (ICI.isEquality() && LHSI->hasOneUse()) {
1058  // Simplify icmp eq (trunc x to i8), 42 -> icmp eq x, 42|highbits if all
1059  // of the high bits truncated out of x are known.
1060  unsigned DstBits = LHSI->getType()->getPrimitiveSizeInBits(),
1061  SrcBits = LHSI->getOperand(0)->getType()->getPrimitiveSizeInBits();
1062  APInt KnownZero(SrcBits, 0), KnownOne(SrcBits, 0);
1063  computeKnownBits(LHSI->getOperand(0), KnownZero, KnownOne);
1064 
1065  // If all the high bits are known, we can do this xform.
1066  if ((KnownZero|KnownOne).countLeadingOnes() >= SrcBits-DstBits) {
1067  // Pull in the high bits from known-ones set.
1068  APInt NewRHS = RHS->getValue().zext(SrcBits);
1069  NewRHS |= KnownOne & APInt::getHighBitsSet(SrcBits, SrcBits-DstBits);
1070  return new ICmpInst(ICI.getPredicate(), LHSI->getOperand(0),
1071  Builder->getInt(NewRHS));
1072  }
1073  }
1074  break;
1075 
1076  case Instruction::Xor: // (icmp pred (xor X, XorCst), CI)
1077  if (ConstantInt *XorCst = dyn_cast<ConstantInt>(LHSI->getOperand(1))) {
1078  // If this is a comparison that tests the signbit (X < 0) or (x > -1),
1079  // fold the xor.
1080  if ((ICI.getPredicate() == ICmpInst::ICMP_SLT && RHSV == 0) ||
1081  (ICI.getPredicate() == ICmpInst::ICMP_SGT && RHSV.isAllOnesValue())) {
1082  Value *CompareVal = LHSI->getOperand(0);
1083 
1084  // If the sign bit of the XorCst is not set, there is no change to
1085  // the operation, just stop using the Xor.
1086  if (!XorCst->isNegative()) {
1087  ICI.setOperand(0, CompareVal);
1088  Worklist.Add(LHSI);
1089  return &ICI;
1090  }
1091 
1092  // Was the old condition true if the operand is positive?
1093  bool isTrueIfPositive = ICI.getPredicate() == ICmpInst::ICMP_SGT;
1094 
1095  // If so, the new one isn't.
1096  isTrueIfPositive ^= true;
1097 
1098  if (isTrueIfPositive)
1099  return new ICmpInst(ICmpInst::ICMP_SGT, CompareVal,
1100  SubOne(RHS));
1101  else
1102  return new ICmpInst(ICmpInst::ICMP_SLT, CompareVal,
1103  AddOne(RHS));
1104  }
1105 
1106  if (LHSI->hasOneUse()) {
1107  // (icmp u/s (xor A SignBit), C) -> (icmp s/u A, (xor C SignBit))
1108  if (!ICI.isEquality() && XorCst->getValue().isSignBit()) {
1109  const APInt &SignBit = XorCst->getValue();
1110  ICmpInst::Predicate Pred = ICI.isSigned()
1111  ? ICI.getUnsignedPredicate()
1112  : ICI.getSignedPredicate();
1113  return new ICmpInst(Pred, LHSI->getOperand(0),
1114  Builder->getInt(RHSV ^ SignBit));
1115  }
1116 
1117  // (icmp u/s (xor A ~SignBit), C) -> (icmp s/u (xor C ~SignBit), A)
1118  if (!ICI.isEquality() && XorCst->isMaxValue(true)) {
1119  const APInt &NotSignBit = XorCst->getValue();
1120  ICmpInst::Predicate Pred = ICI.isSigned()
1121  ? ICI.getUnsignedPredicate()
1122  : ICI.getSignedPredicate();
1123  Pred = ICI.getSwappedPredicate(Pred);
1124  return new ICmpInst(Pred, LHSI->getOperand(0),
1125  Builder->getInt(RHSV ^ NotSignBit));
1126  }
1127  }
1128 
1129  // (icmp ugt (xor X, C), ~C) -> (icmp ult X, C)
1130  // iff -C is a power of 2
1131  if (ICI.getPredicate() == ICmpInst::ICMP_UGT &&
1132  XorCst->getValue() == ~RHSV && (RHSV + 1).isPowerOf2())
1133  return new ICmpInst(ICmpInst::ICMP_ULT, LHSI->getOperand(0), XorCst);
1134 
1135  // (icmp ult (xor X, C), -C) -> (icmp uge X, C)
1136  // iff -C is a power of 2
1137  if (ICI.getPredicate() == ICmpInst::ICMP_ULT &&
1138  XorCst->getValue() == -RHSV && RHSV.isPowerOf2())
1139  return new ICmpInst(ICmpInst::ICMP_UGE, LHSI->getOperand(0), XorCst);
1140  }
1141  break;
1142  case Instruction::And: // (icmp pred (and X, AndCst), RHS)
1143  if (LHSI->hasOneUse() && isa<ConstantInt>(LHSI->getOperand(1)) &&
1144  LHSI->getOperand(0)->hasOneUse()) {
1145  ConstantInt *AndCst = cast<ConstantInt>(LHSI->getOperand(1));
1146 
1147  // If the LHS is an AND of a truncating cast, we can widen the
1148  // and/compare to be the input width without changing the value
1149  // produced, eliminating a cast.
1150  if (TruncInst *Cast = dyn_cast<TruncInst>(LHSI->getOperand(0))) {
1151  // We can do this transformation if either the AND constant does not
1152  // have its sign bit set or if it is an equality comparison.
1153  // Extending a relational comparison when we're checking the sign
1154  // bit would not work.
1155  if (ICI.isEquality() ||
1156  (!AndCst->isNegative() && RHSV.isNonNegative())) {
1157  Value *NewAnd =
1158  Builder->CreateAnd(Cast->getOperand(0),
1159  ConstantExpr::getZExt(AndCst, Cast->getSrcTy()));
1160  NewAnd->takeName(LHSI);
1161  return new ICmpInst(ICI.getPredicate(), NewAnd,
1162  ConstantExpr::getZExt(RHS, Cast->getSrcTy()));
1163  }
1164  }
1165 
1166  // If the LHS is an AND of a zext, and we have an equality compare, we can
1167  // shrink the and/compare to the smaller type, eliminating the cast.
1168  if (ZExtInst *Cast = dyn_cast<ZExtInst>(LHSI->getOperand(0))) {
1169  IntegerType *Ty = cast<IntegerType>(Cast->getSrcTy());
1170  // Make sure we don't compare the upper bits, SimplifyDemandedBits
1171  // should fold the icmp to true/false in that case.
1172  if (ICI.isEquality() && RHSV.getActiveBits() <= Ty->getBitWidth()) {
1173  Value *NewAnd =
1174  Builder->CreateAnd(Cast->getOperand(0),
1175  ConstantExpr::getTrunc(AndCst, Ty));
1176  NewAnd->takeName(LHSI);
1177  return new ICmpInst(ICI.getPredicate(), NewAnd,
1178  ConstantExpr::getTrunc(RHS, Ty));
1179  }
1180  }
1181 
1182  // If this is: (X >> C1) & C2 != C3 (where any shift and any compare
1183  // could exist), turn it into (X & (C2 << C1)) != (C3 << C1). This
1184  // happens a LOT in code produced by the C front-end, for bitfield
1185  // access.
1186  BinaryOperator *Shift = dyn_cast<BinaryOperator>(LHSI->getOperand(0));
1187  if (Shift && !Shift->isShift())
1188  Shift = nullptr;
1189 
1190  ConstantInt *ShAmt;
1191  ShAmt = Shift ? dyn_cast<ConstantInt>(Shift->getOperand(1)) : nullptr;
1192 
1193  // This seemingly simple opportunity to fold away a shift turns out to
1194  // be rather complicated. See PR17827
1195  // ( http://llvm.org/bugs/show_bug.cgi?id=17827 ) for details.
1196  if (ShAmt) {
1197  bool CanFold = false;
1198  unsigned ShiftOpcode = Shift->getOpcode();
1199  if (ShiftOpcode == Instruction::AShr) {
1200  // There may be some constraints that make this possible,
1201  // but nothing simple has been discovered yet.
1202  CanFold = false;
1203  } else if (ShiftOpcode == Instruction::Shl) {
1204  // For a left shift, we can fold if the comparison is not signed.
1205  // We can also fold a signed comparison if the mask value and
1206  // comparison value are not negative. These constraints may not be
1207  // obvious, but we can prove that they are correct using an SMT
1208  // solver.
1209  if (!ICI.isSigned() || (!AndCst->isNegative() && !RHS->isNegative()))
1210  CanFold = true;
1211  } else if (ShiftOpcode == Instruction::LShr) {
1212  // For a logical right shift, we can fold if the comparison is not
1213  // signed. We can also fold a signed comparison if the shifted mask
1214  // value and the shifted comparison value are not negative.
1215  // These constraints may not be obvious, but we can prove that they
1216  // are correct using an SMT solver.
1217  if (!ICI.isSigned())
1218  CanFold = true;
1219  else {
1220  ConstantInt *ShiftedAndCst =
1221  cast<ConstantInt>(ConstantExpr::getShl(AndCst, ShAmt));
1222  ConstantInt *ShiftedRHSCst =
1223  cast<ConstantInt>(ConstantExpr::getShl(RHS, ShAmt));
1224 
1225  if (!ShiftedAndCst->isNegative() && !ShiftedRHSCst->isNegative())
1226  CanFold = true;
1227  }
1228  }
1229 
1230  if (CanFold) {
1231  Constant *NewCst;
1232  if (ShiftOpcode == Instruction::Shl)
1233  NewCst = ConstantExpr::getLShr(RHS, ShAmt);
1234  else
1235  NewCst = ConstantExpr::getShl(RHS, ShAmt);
1236 
1237  // Check to see if we are shifting out any of the bits being
1238  // compared.
1239  if (ConstantExpr::get(ShiftOpcode, NewCst, ShAmt) != RHS) {
1240  // If we shifted bits out, the fold is not going to work out.
1241  // As a special case, check to see if this means that the
1242  // result is always true or false now.
1243  if (ICI.getPredicate() == ICmpInst::ICMP_EQ)
1244  return ReplaceInstUsesWith(ICI, Builder->getFalse());
1245  if (ICI.getPredicate() == ICmpInst::ICMP_NE)
1246  return ReplaceInstUsesWith(ICI, Builder->getTrue());
1247  } else {
1248  ICI.setOperand(1, NewCst);
1249  Constant *NewAndCst;
1250  if (ShiftOpcode == Instruction::Shl)
1251  NewAndCst = ConstantExpr::getLShr(AndCst, ShAmt);
1252  else
1253  NewAndCst = ConstantExpr::getShl(AndCst, ShAmt);
1254  LHSI->setOperand(1, NewAndCst);
1255  LHSI->setOperand(0, Shift->getOperand(0));
1256  Worklist.Add(Shift); // Shift is dead.
1257  return &ICI;
1258  }
1259  }
1260  }
1261 
1262  // Turn ((X >> Y) & C) == 0 into (X & (C << Y)) == 0. The later is
1263  // preferable because it allows the C<<Y expression to be hoisted out
1264  // of a loop if Y is invariant and X is not.
1265  if (Shift && Shift->hasOneUse() && RHSV == 0 &&
1266  ICI.isEquality() && !Shift->isArithmeticShift() &&
1267  !isa<Constant>(Shift->getOperand(0))) {
1268  // Compute C << Y.
1269  Value *NS;
1270  if (Shift->getOpcode() == Instruction::LShr) {
1271  NS = Builder->CreateShl(AndCst, Shift->getOperand(1));
1272  } else {
1273  // Insert a logical shift.
1274  NS = Builder->CreateLShr(AndCst, Shift->getOperand(1));
1275  }
1276 
1277  // Compute X & (C << Y).
1278  Value *NewAnd =
1279  Builder->CreateAnd(Shift->getOperand(0), NS, LHSI->getName());
1280 
1281  ICI.setOperand(0, NewAnd);
1282  return &ICI;
1283  }
1284 
1285  // Replace ((X & AndCst) > RHSV) with ((X & AndCst) != 0), if any
1286  // bit set in (X & AndCst) will produce a result greater than RHSV.
1287  if (ICI.getPredicate() == ICmpInst::ICMP_UGT) {
1288  unsigned NTZ = AndCst->getValue().countTrailingZeros();
1289  if ((NTZ < AndCst->getBitWidth()) &&
1290  APInt::getOneBitSet(AndCst->getBitWidth(), NTZ).ugt(RHSV))
1291  return new ICmpInst(ICmpInst::ICMP_NE, LHSI,
1293  }
1294  }
1295 
1296  // Try to optimize things like "A[i]&42 == 0" to index computations.
1297  if (LoadInst *LI = dyn_cast<LoadInst>(LHSI->getOperand(0))) {
1298  if (GetElementPtrInst *GEP =
1299  dyn_cast<GetElementPtrInst>(LI->getOperand(0)))
1300  if (GlobalVariable *GV = dyn_cast<GlobalVariable>(GEP->getOperand(0)))
1301  if (GV->isConstant() && GV->hasDefinitiveInitializer() &&
1302  !LI->isVolatile() && isa<ConstantInt>(LHSI->getOperand(1))) {
1303  ConstantInt *C = cast<ConstantInt>(LHSI->getOperand(1));
1304  if (Instruction *Res = FoldCmpLoadFromIndexedGlobal(GEP, GV,ICI, C))
1305  return Res;
1306  }
1307  }
1308 
1309  // X & -C == -C -> X > u ~C
1310  // X & -C != -C -> X <= u ~C
1311  // iff C is a power of 2
1312  if (ICI.isEquality() && RHS == LHSI->getOperand(1) && (-RHSV).isPowerOf2())
1313  return new ICmpInst(
1316  LHSI->getOperand(0), SubOne(RHS));
1317  break;
1318 
1319  case Instruction::Or: {
1320  if (!ICI.isEquality() || !RHS->isNullValue() || !LHSI->hasOneUse())
1321  break;
1322  Value *P, *Q;
1323  if (match(LHSI, m_Or(m_PtrToInt(m_Value(P)), m_PtrToInt(m_Value(Q))))) {
1324  // Simplify icmp eq (or (ptrtoint P), (ptrtoint Q)), 0
1325  // -> and (icmp eq P, null), (icmp eq Q, null).
1326  Value *ICIP = Builder->CreateICmp(ICI.getPredicate(), P,
1328  Value *ICIQ = Builder->CreateICmp(ICI.getPredicate(), Q,
1330  Instruction *Op;
1331  if (ICI.getPredicate() == ICmpInst::ICMP_EQ)
1332  Op = BinaryOperator::CreateAnd(ICIP, ICIQ);
1333  else
1334  Op = BinaryOperator::CreateOr(ICIP, ICIQ);
1335  return Op;
1336  }
1337  break;
1338  }
1339 
1340  case Instruction::Mul: { // (icmp pred (mul X, Val), CI)
1341  ConstantInt *Val = dyn_cast<ConstantInt>(LHSI->getOperand(1));
1342  if (!Val) break;
1343 
1344  // If this is a signed comparison to 0 and the mul is sign preserving,
1345  // use the mul LHS operand instead.
1346  ICmpInst::Predicate pred = ICI.getPredicate();
1347  if (isSignTest(pred, RHS) && !Val->isZero() &&
1348  cast<BinaryOperator>(LHSI)->hasNoSignedWrap())
1349  return new ICmpInst(Val->isNegative() ?
1350  ICmpInst::getSwappedPredicate(pred) : pred,
1351  LHSI->getOperand(0),
1352  Constant::getNullValue(RHS->getType()));
1353 
1354  break;
1355  }
1356 
1357  case Instruction::Shl: { // (icmp pred (shl X, ShAmt), CI)
1358  uint32_t TypeBits = RHSV.getBitWidth();
1359  ConstantInt *ShAmt = dyn_cast<ConstantInt>(LHSI->getOperand(1));
1360  if (!ShAmt) {
1361  Value *X;
1362  // (1 << X) pred P2 -> X pred Log2(P2)
1363  if (match(LHSI, m_Shl(m_One(), m_Value(X)))) {
1364  bool RHSVIsPowerOf2 = RHSV.isPowerOf2();
1365  ICmpInst::Predicate Pred = ICI.getPredicate();
1366  if (ICI.isUnsigned()) {
1367  if (!RHSVIsPowerOf2) {
1368  // (1 << X) < 30 -> X <= 4
1369  // (1 << X) <= 30 -> X <= 4
1370  // (1 << X) >= 30 -> X > 4
1371  // (1 << X) > 30 -> X > 4
1372  if (Pred == ICmpInst::ICMP_ULT)
1373  Pred = ICmpInst::ICMP_ULE;
1374  else if (Pred == ICmpInst::ICMP_UGE)
1375  Pred = ICmpInst::ICMP_UGT;
1376  }
1377  unsigned RHSLog2 = RHSV.logBase2();
1378 
1379  // (1 << X) >= 2147483648 -> X >= 31 -> X == 31
1380  // (1 << X) > 2147483648 -> X > 31 -> false
1381  // (1 << X) <= 2147483648 -> X <= 31 -> true
1382  // (1 << X) < 2147483648 -> X < 31 -> X != 31
1383  if (RHSLog2 == TypeBits-1) {
1384  if (Pred == ICmpInst::ICMP_UGE)
1385  Pred = ICmpInst::ICMP_EQ;
1386  else if (Pred == ICmpInst::ICMP_UGT)
1387  return ReplaceInstUsesWith(ICI, Builder->getFalse());
1388  else if (Pred == ICmpInst::ICMP_ULE)
1389  return ReplaceInstUsesWith(ICI, Builder->getTrue());
1390  else if (Pred == ICmpInst::ICMP_ULT)
1391  Pred = ICmpInst::ICMP_NE;
1392  }
1393 
1394  return new ICmpInst(Pred, X,
1395  ConstantInt::get(RHS->getType(), RHSLog2));
1396  } else if (ICI.isSigned()) {
1397  if (RHSV.isAllOnesValue()) {
1398  // (1 << X) <= -1 -> X == 31
1399  if (Pred == ICmpInst::ICMP_SLE)
1400  return new ICmpInst(ICmpInst::ICMP_EQ, X,
1401  ConstantInt::get(RHS->getType(), TypeBits-1));
1402 
1403  // (1 << X) > -1 -> X != 31
1404  if (Pred == ICmpInst::ICMP_SGT)
1405  return new ICmpInst(ICmpInst::ICMP_NE, X,
1406  ConstantInt::get(RHS->getType(), TypeBits-1));
1407  } else if (!RHSV) {
1408  // (1 << X) < 0 -> X == 31
1409  // (1 << X) <= 0 -> X == 31
1410  if (Pred == ICmpInst::ICMP_SLT || Pred == ICmpInst::ICMP_SLE)
1411  return new ICmpInst(ICmpInst::ICMP_EQ, X,
1412  ConstantInt::get(RHS->getType(), TypeBits-1));
1413 
1414  // (1 << X) >= 0 -> X != 31
1415  // (1 << X) > 0 -> X != 31
1416  if (Pred == ICmpInst::ICMP_SGT || Pred == ICmpInst::ICMP_SGE)
1417  return new ICmpInst(ICmpInst::ICMP_NE, X,
1418  ConstantInt::get(RHS->getType(), TypeBits-1));
1419  }
1420  } else if (ICI.isEquality()) {
1421  if (RHSVIsPowerOf2)
1422  return new ICmpInst(
1423  Pred, X, ConstantInt::get(RHS->getType(), RHSV.logBase2()));
1424 
1425  return ReplaceInstUsesWith(
1426  ICI, Pred == ICmpInst::ICMP_EQ ? Builder->getFalse()
1427  : Builder->getTrue());
1428  }
1429  }
1430  break;
1431  }
1432 
1433  // Check that the shift amount is in range. If not, don't perform
1434  // undefined shifts. When the shift is visited it will be
1435  // simplified.
1436  if (ShAmt->uge(TypeBits))
1437  break;
1438 
1439  if (ICI.isEquality()) {
1440  // If we are comparing against bits always shifted out, the
1441  // comparison cannot succeed.
1442  Constant *Comp =
1444  ShAmt);
1445  if (Comp != RHS) {// Comparing against a bit that we know is zero.
1446  bool IsICMP_NE = ICI.getPredicate() == ICmpInst::ICMP_NE;
1447  Constant *Cst = Builder->getInt1(IsICMP_NE);
1448  return ReplaceInstUsesWith(ICI, Cst);
1449  }
1450 
1451  // If the shift is NUW, then it is just shifting out zeros, no need for an
1452  // AND.
1453  if (cast<BinaryOperator>(LHSI)->hasNoUnsignedWrap())
1454  return new ICmpInst(ICI.getPredicate(), LHSI->getOperand(0),
1455  ConstantExpr::getLShr(RHS, ShAmt));
1456 
1457  // If the shift is NSW and we compare to 0, then it is just shifting out
1458  // sign bits, no need for an AND either.
1459  if (cast<BinaryOperator>(LHSI)->hasNoSignedWrap() && RHSV == 0)
1460  return new ICmpInst(ICI.getPredicate(), LHSI->getOperand(0),
1461  ConstantExpr::getLShr(RHS, ShAmt));
1462 
1463  if (LHSI->hasOneUse()) {
1464  // Otherwise strength reduce the shift into an and.
1465  uint32_t ShAmtVal = (uint32_t)ShAmt->getLimitedValue(TypeBits);
1466  Constant *Mask = Builder->getInt(APInt::getLowBitsSet(TypeBits,
1467  TypeBits - ShAmtVal));
1468 
1469  Value *And =
1470  Builder->CreateAnd(LHSI->getOperand(0),Mask, LHSI->getName()+".mask");
1471  return new ICmpInst(ICI.getPredicate(), And,
1472  ConstantExpr::getLShr(RHS, ShAmt));
1473  }
1474  }
1475 
1476  // If this is a signed comparison to 0 and the shift is sign preserving,
1477  // use the shift LHS operand instead.
1478  ICmpInst::Predicate pred = ICI.getPredicate();
1479  if (isSignTest(pred, RHS) &&
1480  cast<BinaryOperator>(LHSI)->hasNoSignedWrap())
1481  return new ICmpInst(pred,
1482  LHSI->getOperand(0),
1484 
1485  // Otherwise, if this is a comparison of the sign bit, simplify to and/test.
1486  bool TrueIfSigned = false;
1487  if (LHSI->hasOneUse() &&
1488  isSignBitCheck(ICI.getPredicate(), RHS, TrueIfSigned)) {
1489  // (X << 31) <s 0 --> (X&1) != 0
1490  Constant *Mask = ConstantInt::get(LHSI->getOperand(0)->getType(),
1491  APInt::getOneBitSet(TypeBits,
1492  TypeBits-ShAmt->getZExtValue()-1));
1493  Value *And =
1494  Builder->CreateAnd(LHSI->getOperand(0), Mask, LHSI->getName()+".mask");
1495  return new ICmpInst(TrueIfSigned ? ICmpInst::ICMP_NE : ICmpInst::ICMP_EQ,
1496  And, Constant::getNullValue(And->getType()));
1497  }
1498 
1499  // Transform (icmp pred iM (shl iM %v, N), CI)
1500  // -> (icmp pred i(M-N) (trunc %v iM to i(M-N)), (trunc (CI>>N))
1501  // Transform the shl to a trunc if (trunc (CI>>N)) has no loss and M-N.
1502  // This enables to get rid of the shift in favor of a trunc which can be
1503  // free on the target. It has the additional benefit of comparing to a
1504  // smaller constant, which will be target friendly.
1505  unsigned Amt = ShAmt->getLimitedValue(TypeBits-1);
1506  if (LHSI->hasOneUse() &&
1507  Amt != 0 && RHSV.countTrailingZeros() >= Amt) {
1508  Type *NTy = IntegerType::get(ICI.getContext(), TypeBits - Amt);
1511  ConstantInt::get(RHS->getType(), Amt)),
1512  NTy);
1513  return new ICmpInst(ICI.getPredicate(),
1514  Builder->CreateTrunc(LHSI->getOperand(0), NTy),
1515  NCI);
1516  }
1517 
1518  break;
1519  }
1520 
1521  case Instruction::LShr: // (icmp pred (shr X, ShAmt), CI)
1522  case Instruction::AShr: {
1523  // Handle equality comparisons of shift-by-constant.
1524  BinaryOperator *BO = cast<BinaryOperator>(LHSI);
1525  if (ConstantInt *ShAmt = dyn_cast<ConstantInt>(LHSI->getOperand(1))) {
1526  if (Instruction *Res = FoldICmpShrCst(ICI, BO, ShAmt))
1527  return Res;
1528  }
1529 
1530  // Handle exact shr's.
1531  if (ICI.isEquality() && BO->isExact() && BO->hasOneUse()) {
1532  if (RHSV.isMinValue())
1533  return new ICmpInst(ICI.getPredicate(), BO->getOperand(0), RHS);
1534  }
1535  break;
1536  }
1537 
1538  case Instruction::SDiv:
1539  case Instruction::UDiv:
1540  // Fold: icmp pred ([us]div X, C1), C2 -> range test
1541  // Fold this div into the comparison, producing a range check.
1542  // Determine, based on the divide type, what the range is being
1543  // checked. If there is an overflow on the low or high side, remember
1544  // it, otherwise compute the range [low, hi) bounding the new value.
1545  // See: InsertRangeTest above for the kinds of replacements possible.
1546  if (ConstantInt *DivRHS = dyn_cast<ConstantInt>(LHSI->getOperand(1)))
1547  if (Instruction *R = FoldICmpDivCst(ICI, cast<BinaryOperator>(LHSI),
1548  DivRHS))
1549  return R;
1550  break;
1551 
1552  case Instruction::Sub: {
1553  ConstantInt *LHSC = dyn_cast<ConstantInt>(LHSI->getOperand(0));
1554  if (!LHSC) break;
1555  const APInt &LHSV = LHSC->getValue();
1556 
1557  // C1-X <u C2 -> (X|(C2-1)) == C1
1558  // iff C1 & (C2-1) == C2-1
1559  // C2 is a power of 2
1560  if (ICI.getPredicate() == ICmpInst::ICMP_ULT && LHSI->hasOneUse() &&
1561  RHSV.isPowerOf2() && (LHSV & (RHSV - 1)) == (RHSV - 1))
1562  return new ICmpInst(ICmpInst::ICMP_EQ,
1563  Builder->CreateOr(LHSI->getOperand(1), RHSV - 1),
1564  LHSC);
1565 
1566  // C1-X >u C2 -> (X|C2) != C1
1567  // iff C1 & C2 == C2
1568  // C2+1 is a power of 2
1569  if (ICI.getPredicate() == ICmpInst::ICMP_UGT && LHSI->hasOneUse() &&
1570  (RHSV + 1).isPowerOf2() && (LHSV & RHSV) == RHSV)
1571  return new ICmpInst(ICmpInst::ICMP_NE,
1572  Builder->CreateOr(LHSI->getOperand(1), RHSV), LHSC);
1573  break;
1574  }
1575 
1576  case Instruction::Add:
1577  // Fold: icmp pred (add X, C1), C2
1578  if (!ICI.isEquality()) {
1579  ConstantInt *LHSC = dyn_cast<ConstantInt>(LHSI->getOperand(1));
1580  if (!LHSC) break;
1581  const APInt &LHSV = LHSC->getValue();
1582 
1583  ConstantRange CR = ICI.makeConstantRange(ICI.getPredicate(), RHSV)
1584  .subtract(LHSV);
1585 
1586  if (ICI.isSigned()) {
1587  if (CR.getLower().isSignBit()) {
1588  return new ICmpInst(ICmpInst::ICMP_SLT, LHSI->getOperand(0),
1589  Builder->getInt(CR.getUpper()));
1590  } else if (CR.getUpper().isSignBit()) {
1591  return new ICmpInst(ICmpInst::ICMP_SGE, LHSI->getOperand(0),
1592  Builder->getInt(CR.getLower()));
1593  }
1594  } else {
1595  if (CR.getLower().isMinValue()) {
1596  return new ICmpInst(ICmpInst::ICMP_ULT, LHSI->getOperand(0),
1597  Builder->getInt(CR.getUpper()));
1598  } else if (CR.getUpper().isMinValue()) {
1599  return new ICmpInst(ICmpInst::ICMP_UGE, LHSI->getOperand(0),
1600  Builder->getInt(CR.getLower()));
1601  }
1602  }
1603 
1604  // X-C1 <u C2 -> (X & -C2) == C1
1605  // iff C1 & (C2-1) == 0
1606  // C2 is a power of 2
1607  if (ICI.getPredicate() == ICmpInst::ICMP_ULT && LHSI->hasOneUse() &&
1608  RHSV.isPowerOf2() && (LHSV & (RHSV - 1)) == 0)
1609  return new ICmpInst(ICmpInst::ICMP_EQ,
1610  Builder->CreateAnd(LHSI->getOperand(0), -RHSV),
1611  ConstantExpr::getNeg(LHSC));
1612 
1613  // X-C1 >u C2 -> (X & ~C2) != C1
1614  // iff C1 & C2 == 0
1615  // C2+1 is a power of 2
1616  if (ICI.getPredicate() == ICmpInst::ICMP_UGT && LHSI->hasOneUse() &&
1617  (RHSV + 1).isPowerOf2() && (LHSV & RHSV) == 0)
1618  return new ICmpInst(ICmpInst::ICMP_NE,
1619  Builder->CreateAnd(LHSI->getOperand(0), ~RHSV),
1620  ConstantExpr::getNeg(LHSC));
1621  }
1622  break;
1623  }
1624 
1625  // Simplify icmp_eq and icmp_ne instructions with integer constant RHS.
1626  if (ICI.isEquality()) {
1627  bool isICMP_NE = ICI.getPredicate() == ICmpInst::ICMP_NE;
1628 
1629  // If the first operand is (add|sub|and|or|xor|rem) with a constant, and
1630  // the second operand is a constant, simplify a bit.
1631  if (BinaryOperator *BO = dyn_cast<BinaryOperator>(LHSI)) {
1632  switch (BO->getOpcode()) {
1633  case Instruction::SRem:
1634  // If we have a signed (X % (2^c)) == 0, turn it into an unsigned one.
1635  if (RHSV == 0 && isa<ConstantInt>(BO->getOperand(1)) &&BO->hasOneUse()){
1636  const APInt &V = cast<ConstantInt>(BO->getOperand(1))->getValue();
1637  if (V.sgt(1) && V.isPowerOf2()) {
1638  Value *NewRem =
1639  Builder->CreateURem(BO->getOperand(0), BO->getOperand(1),
1640  BO->getName());
1641  return new ICmpInst(ICI.getPredicate(), NewRem,
1643  }
1644  }
1645  break;
1646  case Instruction::Add:
1647  // Replace ((add A, B) != C) with (A != C-B) if B & C are constants.
1648  if (ConstantInt *BOp1C = dyn_cast<ConstantInt>(BO->getOperand(1))) {
1649  if (BO->hasOneUse())
1650  return new ICmpInst(ICI.getPredicate(), BO->getOperand(0),
1651  ConstantExpr::getSub(RHS, BOp1C));
1652  } else if (RHSV == 0) {
1653  // Replace ((add A, B) != 0) with (A != -B) if A or B is
1654  // efficiently invertible, or if the add has just this one use.
1655  Value *BOp0 = BO->getOperand(0), *BOp1 = BO->getOperand(1);
1656 
1657  if (Value *NegVal = dyn_castNegVal(BOp1))
1658  return new ICmpInst(ICI.getPredicate(), BOp0, NegVal);
1659  if (Value *NegVal = dyn_castNegVal(BOp0))
1660  return new ICmpInst(ICI.getPredicate(), NegVal, BOp1);
1661  if (BO->hasOneUse()) {
1662  Value *Neg = Builder->CreateNeg(BOp1);
1663  Neg->takeName(BO);
1664  return new ICmpInst(ICI.getPredicate(), BOp0, Neg);
1665  }
1666  }
1667  break;
1668  case Instruction::Xor:
1669  // For the xor case, we can xor two constants together, eliminating
1670  // the explicit xor.
1671  if (Constant *BOC = dyn_cast<Constant>(BO->getOperand(1))) {
1672  return new ICmpInst(ICI.getPredicate(), BO->getOperand(0),
1673  ConstantExpr::getXor(RHS, BOC));
1674  } else if (RHSV == 0) {
1675  // Replace ((xor A, B) != 0) with (A != B)
1676  return new ICmpInst(ICI.getPredicate(), BO->getOperand(0),
1677  BO->getOperand(1));
1678  }
1679  break;
1680  case Instruction::Sub:
1681  // Replace ((sub A, B) != C) with (B != A-C) if A & C are constants.
1682  if (ConstantInt *BOp0C = dyn_cast<ConstantInt>(BO->getOperand(0))) {
1683  if (BO->hasOneUse())
1684  return new ICmpInst(ICI.getPredicate(), BO->getOperand(1),
1685  ConstantExpr::getSub(BOp0C, RHS));
1686  } else if (RHSV == 0) {
1687  // Replace ((sub A, B) != 0) with (A != B)
1688  return new ICmpInst(ICI.getPredicate(), BO->getOperand(0),
1689  BO->getOperand(1));
1690  }
1691  break;
1692  case Instruction::Or:
1693  // If bits are being or'd in that are not present in the constant we
1694  // are comparing against, then the comparison could never succeed!
1695  if (ConstantInt *BOC = dyn_cast<ConstantInt>(BO->getOperand(1))) {
1696  Constant *NotCI = ConstantExpr::getNot(RHS);
1697  if (!ConstantExpr::getAnd(BOC, NotCI)->isNullValue())
1698  return ReplaceInstUsesWith(ICI, Builder->getInt1(isICMP_NE));
1699  }
1700  break;
1701 
1702  case Instruction::And:
1703  if (ConstantInt *BOC = dyn_cast<ConstantInt>(BO->getOperand(1))) {
1704  // If bits are being compared against that are and'd out, then the
1705  // comparison can never succeed!
1706  if ((RHSV & ~BOC->getValue()) != 0)
1707  return ReplaceInstUsesWith(ICI, Builder->getInt1(isICMP_NE));
1708 
1709  // If we have ((X & C) == C), turn it into ((X & C) != 0).
1710  if (RHS == BOC && RHSV.isPowerOf2())
1711  return new ICmpInst(isICMP_NE ? ICmpInst::ICMP_EQ :
1712  ICmpInst::ICMP_NE, LHSI,
1714 
1715  // Don't perform the following transforms if the AND has multiple uses
1716  if (!BO->hasOneUse())
1717  break;
1718 
1719  // Replace (and X, (1 << size(X)-1) != 0) with x s< 0
1720  if (BOC->getValue().isSignBit()) {
1721  Value *X = BO->getOperand(0);
1722  Constant *Zero = Constant::getNullValue(X->getType());
1723  ICmpInst::Predicate pred = isICMP_NE ?
1725  return new ICmpInst(pred, X, Zero);
1726  }
1727 
1728  // ((X & ~7) == 0) --> X < 8
1729  if (RHSV == 0 && isHighOnes(BOC)) {
1730  Value *X = BO->getOperand(0);
1731  Constant *NegX = ConstantExpr::getNeg(BOC);
1732  ICmpInst::Predicate pred = isICMP_NE ?
1734  return new ICmpInst(pred, X, NegX);
1735  }
1736  }
1737  break;
1738  case Instruction::Mul:
1739  if (RHSV == 0 && BO->hasNoSignedWrap()) {
1740  if (ConstantInt *BOC = dyn_cast<ConstantInt>(BO->getOperand(1))) {
1741  // The trivial case (mul X, 0) is handled by InstSimplify
1742  // General case : (mul X, C) != 0 iff X != 0
1743  // (mul X, C) == 0 iff X == 0
1744  if (!BOC->isZero())
1745  return new ICmpInst(ICI.getPredicate(), BO->getOperand(0),
1747  }
1748  }
1749  break;
1750  default: break;
1751  }
1752  } else if (IntrinsicInst *II = dyn_cast<IntrinsicInst>(LHSI)) {
1753  // Handle icmp {eq|ne} <intrinsic>, intcst.
1754  switch (II->getIntrinsicID()) {
1755  case Intrinsic::bswap:
1756  Worklist.Add(II);
1757  ICI.setOperand(0, II->getArgOperand(0));
1758  ICI.setOperand(1, Builder->getInt(RHSV.byteSwap()));
1759  return &ICI;
1760  case Intrinsic::ctlz:
1761  case Intrinsic::cttz:
1762  // ctz(A) == bitwidth(a) -> A == 0 and likewise for !=
1763  if (RHSV == RHS->getType()->getBitWidth()) {
1764  Worklist.Add(II);
1765  ICI.setOperand(0, II->getArgOperand(0));
1766  ICI.setOperand(1, ConstantInt::get(RHS->getType(), 0));
1767  return &ICI;
1768  }
1769  break;
1770  case Intrinsic::ctpop:
1771  // popcount(A) == 0 -> A == 0 and likewise for !=
1772  if (RHS->isZero()) {
1773  Worklist.Add(II);
1774  ICI.setOperand(0, II->getArgOperand(0));
1775  ICI.setOperand(1, RHS);
1776  return &ICI;
1777  }
1778  break;
1779  default:
1780  break;
1781  }
1782  }
1783  }
1784  return nullptr;
1785 }
Value * CreateLShr(Value *LHS, Value *RHS, const Twine &Name="", bool isExact=false)
Definition: IRBuilder.h:770
bool isArithmeticShift() const
isArithmeticShift - Return true if this is an arithmetic shift right.
Definition: Instruction.h:116
static bool isHighOnes(const ConstantInt *CI)
IntegerType * getType() const
Definition: Constants.h:139
class_match< Value > m_Value()
m_Value() - Match an arbitrary value and ignore it.
Definition: PatternMatch.h:70
Value * CreateICmp(CmpInst::Predicate P, Value *LHS, Value *RHS, const Twine &Name="")
Definition: IRBuilder.h:1321
unsigned getBitWidth() const
getBitWidth - Return the bitwidth of this constant.
Definition: Constants.h:110
This class represents zero extension of integer types.
static APInt getLowBitsSet(unsigned numBits, unsigned loBitsSet)
Get a value with low bits set.
Definition: APInt.h:526
unsigned less or equal
Definition: InstrTypes.h:698
unsigned less than
Definition: InstrTypes.h:697
bool isSigned() const
Determine if this instruction is using a signed comparison.
Definition: InstrTypes.h:801
static RegisterPass< NVPTXAllocaHoisting > X("alloca-hoisting","Hoisting alloca instructions in non-entry ""blocks to the entry block")
void Add(Instruction *I)
static bool isEquality(Predicate P)
unsigned getBitWidth() const
Get the number of bits in this IntegerType.
Definition: DerivedTypes.h:61
FunctionType * getType(LLVMContext &Context, ID id, ArrayRef< Type * > Tys=None)
Definition: Function.cpp:711
static Constant * getSub(Constant *C1, Constant *C2, bool HasNUW=false, bool HasNSW=false)
Definition: Constants.cpp:2113
LoopInfoBase< BlockT, LoopT > * LI
Definition: LoopInfoImpl.h:412
static Constant * getNullValue(Type *Ty)
Definition: Constants.cpp:133
StringRef getName() const
Definition: Value.cpp:168
bool uge(uint64_t Num) const
Determine if the value is greater or equal to the given number.
Definition: Constants.h:209
bool match(Val *V, const Pattern &P)
Definition: PatternMatch.h:42
static unsigned getBitWidth(Type *Ty, const DataLayout *TD)
static Constant * get(unsigned Opcode, Constant *C1, Constant *C2, unsigned Flags=0)
Definition: Constants.cpp:1752
const APInt & getValue() const
Return the constant's value.
Definition: Constants.h:105
static Constant * getLShr(Constant *C1, Constant *C2, bool isExact=false)
Definition: Constants.cpp:2180
static Constant * AddOne(Constant *C)
AddOne - Add one to a Constant.
Definition: InstCombine.h:60
bool isNegative() const
Definition: Constants.h:155
uint64_t getZExtValue() const
Return the zero extended value.
Definition: Constants.h:116
Value * CreateAnd(Value *LHS, Value *RHS, const Twine &Name="")
Definition: IRBuilder.h:806
Value * CreateOr(Value *LHS, Value *RHS, const Twine &Name="")
Definition: IRBuilder.h:822
static Constant * getAShr(Constant *C1, Constant *C2, bool isExact=false)
Definition: Constants.cpp:2185
uint64_t getLimitedValue(uint64_t Limit=~0ULL) const
Get the constant's value with a saturation limit.
Definition: Constants.h:218
bool sgt(const APInt &RHS) const
Signed greather than comparison.
Definition: APInt.h:1096
Predicate getUnsignedPredicate() const
Return the unsigned version of the predicate.
void takeName(Value *V)
Definition: Value.cpp:242
This class represents a truncation of integer types.
static APInt getHighBitsSet(unsigned numBits, unsigned hiBitsSet)
Get a value with high bits set.
Definition: APInt.h:508
Value * dyn_castNegVal(Value *V) const
#define P(N)
static bool isSignTest(ICmpInst::Predicate &pred, const ConstantInt *RHS)
BinaryOp_match< LHS, RHS, Instruction::Or > m_Or(const LHS &L, const RHS &R)
Definition: PatternMatch.h:473
ConstantInt * getTrue()
Get the constant value for i1 true.
Definition: IRBuilder.h:256
ConstantInt * getInt1(bool V)
Get a constant value representing either true or false.
Definition: IRBuilder.h:251
LLVM Constant Representation.
Definition: Constant.h:41
Instruction * ReplaceInstUsesWith(Instruction &I, Value *V)
Definition: InstCombine.h:282
static Constant * getAnd(Constant *C1, Constant *C2)
Definition: Constants.cpp:2161
APInt Or(const APInt &LHS, const APInt &RHS)
Bitwise OR function for APInt.
Definition: APInt.h:1870
Instruction * FoldICmpShrCst(ICmpInst &ICI, BinaryOperator *DivI, ConstantInt *DivRHS)
FoldICmpShrCst - Handle "icmp(([al]shr X, cst1), cst2)".
APInt Xor(const APInt &LHS, const APInt &RHS)
Bitwise XOR function for APInt.
Definition: APInt.h:1875
static APInt getOneBitSet(unsigned numBits, unsigned BitNo)
Return an APInt with exactly one bit set in the result.
Definition: APInt.h:474
BinaryOp_match< LHS, RHS, Instruction::Shl > m_Shl(const LHS &L, const RHS &R)
Definition: PatternMatch.h:485
Represent an integer comparison operator.
Definition: Instructions.h:977
Value * getOperand(unsigned i) const
Definition: User.h:90
Integer representation type.
Definition: DerivedTypes.h:37
Predicate getPredicate() const
Return the predicate for this instruction.
Definition: InstrTypes.h:735
InstCombineWorklist Worklist
Worklist - All of the instructions that need to be simplified.
Definition: InstCombine.h:97
static Constant * getNot(Constant *C)
Definition: Constants.cpp:2096
BuilderTy * Builder
Definition: InstCombine.h:102
LLVMContext & getContext() const
All values hold a context through their type.
Definition: Value.cpp:615
bool hasNoSignedWrap() const
hasNoSignedWrap - Determine whether the no signed wrap flag is set.
bool isPowerOf2() const
Check if this APInt's value is a power of two greater than zero.
Definition: APInt.h:388
signed greater than
Definition: InstrTypes.h:699
unsigned countTrailingZeros() const
Count the number of trailing zero bits.
Definition: APInt.cpp:737
BinaryOps getOpcode() const
Definition: InstrTypes.h:326
static IntegerType * get(LLVMContext &C, unsigned NumBits)
Get or create an IntegerType instance.
Definition: Type.cpp:299
Class for constant integers.
Definition: Constants.h:51
Type * getType() const
Definition: Value.h:215
signed less than
Definition: InstrTypes.h:701
const APInt & getLower() const
Definition: ConstantRange.h:74
Predicate getSwappedPredicate() const
Return the predicate as if the operands were swapped.
Definition: InstrTypes.h:774
static Constant * get(Type *Ty, uint64_t V, bool isSigned=false)
Definition: Constants.cpp:528
bool isZero() const
Definition: Constants.h:160
static Constant * getTrunc(Constant *C, Type *Ty)
Definition: Constants.cpp:1587
bool isNullValue() const
Definition: Constants.cpp:75
bool isExact() const
isExact - Determine whether the exact flag is set.
void setOperand(unsigned i, Value *Val)
Definition: User.h:94
signed less or equal
Definition: InstrTypes.h:702
Value * CreateURem(Value *LHS, Value *RHS, const Twine &Name="")
Definition: IRBuilder.h:730
Class for arbitrary precision integers.
Definition: APInt.h:75
CastClass_match< OpTy, Instruction::PtrToInt > m_PtrToInt(const OpTy &Op)
m_PtrToInt
Definition: PatternMatch.h:761
LLVM_ATTRIBUTE_UNUSED_RESULT std::enable_if< !is_simple_type< Y >::value, typename cast_retty< X, const Y >::ret_type >::type dyn_cast(const Y &Val)
Definition: Casting.h:265
bool isMinValue() const
Determine if this is the smallest unsigned value.
Definition: APInt.h:363
APInt And(const APInt &LHS, const APInt &RHS)
Bitwise AND function for APInt.
Definition: APInt.h:1865
ConstantInt * getFalse()
Get the constant value for i1 false.
Definition: IRBuilder.h:261
Value * CreateShl(Value *LHS, Value *RHS, const Twine &Name="", bool HasNUW=false, bool HasNSW=false)
Definition: IRBuilder.h:751
static Constant * getNeg(Constant *C, bool HasNUW=false, bool HasNSW=false)
Definition: Constants.cpp:2083
static bool isSignBitCheck(ICmpInst::Predicate pred, ConstantInt *RHS, bool &TrueIfSigned)
static Constant * getZExt(Constant *C, Type *Ty)
Definition: Constants.cpp:1615
unsigned greater or equal
Definition: InstrTypes.h:696
bool hasOneUse() const
Definition: Value.h:284
bool isSignBit() const
Check if the APInt's value is returned by getSignBit.
Definition: APInt.h:397
static ConstantRange makeConstantRange(Predicate pred, const APInt &C)
Make a ConstantRange for a relation with a constant value.
static Constant * getShl(Constant *C1, Constant *C2, bool HasNUW=false, bool HasNSW=false)
Definition: Constants.cpp:2173
Instruction * FoldICmpDivCst(ICmpInst &ICI, BinaryOperator *DivI, ConstantInt *DivRHS)
Instruction * FoldCmpLoadFromIndexedGlobal(GetElementPtrInst *GEP, GlobalVariable *GV, CmpInst &ICI, ConstantInt *AndCst=nullptr)
bool isUnsigned() const
Determine if this instruction is using an unsigned comparison.
Definition: InstrTypes.h:807
const APInt & getUpper() const
Definition: ConstantRange.h:78
Value * CreateNeg(Value *V, const Twine &Name="", bool HasNUW=false, bool HasNSW=false)
Definition: IRBuilder.h:863
unsigned getPrimitiveSizeInBits() const LLVM_READONLY
Definition: Type.cpp:117
Value * CreateTrunc(Value *V, Type *DestTy, const Twine &Name="")
Definition: IRBuilder.h:1094
LLVM Value Representation.
Definition: Value.h:69
unsigned getOpcode() const
getOpcode() returns a member of one of the enums like Instruction::Add.
Definition: Instruction.h:86
cst_pred_ty< is_one > m_One()
m_One() - Match an integer 1 or a vector with all elements equal to 1.
Definition: PatternMatch.h:257
Predicate getSignedPredicate() const
Return the signed version of the predicate.
unsigned greater than
Definition: InstrTypes.h:695
APInt LLVM_ATTRIBUTE_UNUSED_RESULT zext(unsigned width) const
Zero extend to a new width.
Definition: APInt.cpp:984
void computeKnownBits(Value *V, APInt &KnownZero, APInt &KnownOne, unsigned Depth=0) const
Definition: InstCombine.h:318
ConstantInt * getInt(const APInt &AI)
Get a constant integer value.
Definition: IRBuilder.h:292
static Constant * SubOne(Constant *C)
SubOne - Subtract one from a Constant.
Definition: InstCombine.h:64
signed greater or equal
Definition: InstrTypes.h:700
static Constant * getXor(Constant *C1, Constant *C2)
Definition: Constants.cpp:2169
Instruction * llvm::InstVisitor< InstCombiner , Instruction * >::visitIndirectBrInst ( IndirectBrInst I)
inlineinherited

Definition at line 169 of file InstVisitor.h.

169 { DELEGATE(TerminatorInst);}
#define DELEGATE(CLASS_TO_VISIT)
Definition: InstVisitor.h:30
Instruction * InstCombiner::visitInsertElementInst ( InsertElementInst IE)

Definition at line 528 of file InstCombineVectorOps.cpp.

528  {
529  Value *VecOp = IE.getOperand(0);
530  Value *ScalarOp = IE.getOperand(1);
531  Value *IdxOp = IE.getOperand(2);
532 
533  // Inserting an undef or into an undefined place, remove this.
534  if (isa<UndefValue>(ScalarOp) || isa<UndefValue>(IdxOp))
535  ReplaceInstUsesWith(IE, VecOp);
536 
537  // If the inserted element was extracted from some other vector, and if the
538  // indexes are constant, try to turn this into a shufflevector operation.
539  if (ExtractElementInst *EI = dyn_cast<ExtractElementInst>(ScalarOp)) {
540  if (isa<ConstantInt>(EI->getOperand(1)) && isa<ConstantInt>(IdxOp)) {
541  unsigned NumInsertVectorElts = IE.getType()->getNumElements();
542  unsigned NumExtractVectorElts =
543  EI->getOperand(0)->getType()->getVectorNumElements();
544  unsigned ExtractedIdx =
545  cast<ConstantInt>(EI->getOperand(1))->getZExtValue();
546  unsigned InsertedIdx = cast<ConstantInt>(IdxOp)->getZExtValue();
547 
548  if (ExtractedIdx >= NumExtractVectorElts) // Out of range extract.
549  return ReplaceInstUsesWith(IE, VecOp);
550 
551  if (InsertedIdx >= NumInsertVectorElts) // Out of range insert.
552  return ReplaceInstUsesWith(IE, UndefValue::get(IE.getType()));
553 
554  // If we are extracting a value from a vector, then inserting it right
555  // back into the same place, just use the input vector.
556  if (EI->getOperand(0) == VecOp && ExtractedIdx == InsertedIdx)
557  return ReplaceInstUsesWith(IE, VecOp);
558 
559  // If this insertelement isn't used by some other insertelement, turn it
560  // (and any insertelements it points to), into one big shuffle.
561  if (!IE.hasOneUse() || !isa<InsertElementInst>(IE.user_back())) {
563  ShuffleOps LR = CollectShuffleElements(&IE, Mask, nullptr);
564 
565  // The proposed shuffle may be trivial, in which case we shouldn't
566  // perform the combine.
567  if (LR.first != &IE && LR.second != &IE) {
568  // We now have a shuffle of LHS, RHS, Mask.
569  if (LR.second == nullptr)
570  LR.second = UndefValue::get(LR.first->getType());
571  return new ShuffleVectorInst(LR.first, LR.second,
572  ConstantVector::get(Mask));
573  }
574  }
575  }
576  }
577 
578  unsigned VWidth = cast<VectorType>(VecOp->getType())->getNumElements();
579  APInt UndefElts(VWidth, 0);
580  APInt AllOnesEltMask(APInt::getAllOnesValue(VWidth));
581  if (Value *V = SimplifyDemandedVectorElts(&IE, AllOnesEltMask, UndefElts)) {
582  if (V != &IE)
583  return ReplaceInstUsesWith(IE, V);
584  return &IE;
585  }
586 
587  return nullptr;
588 }
static APInt getAllOnesValue(unsigned numBits)
Get the all-ones value.
Definition: APInt.h:448
Value * SimplifyDemandedVectorElts(Value *V, APInt DemandedElts, APInt &UndefElts, unsigned Depth=0)
std::pair< Value *, Value * > ShuffleOps
static Constant * get(ArrayRef< Constant * > V)
Definition: Constants.cpp:961
VectorType * getType() const
unsigned getNumElements() const
Return the number of elements in the Vector type.
Definition: DerivedTypes.h:428
Instruction * ReplaceInstUsesWith(Instruction &I, Value *V)
Definition: InstCombine.h:282
Value * getOperand(unsigned i) const
Definition: User.h:90
static UndefValue * get(Type *T)
Definition: Constants.cpp:1372
Type * getType() const
Definition: Value.h:215
Instruction * user_back()
Definition: Instruction.h:50
Class for arbitrary precision integers.
Definition: APInt.h:75
bool hasOneUse() const
Definition: Value.h:284
LLVM Value Representation.
Definition: Value.h:69
static ShuffleOps CollectShuffleElements(Value *V, SmallVectorImpl< Constant * > &Mask, Value *PermittedRHS)
Instruction * InstCombiner::visitInsertValueInst ( InsertValueInst I)

Try to find redundant insertvalue instructions, like the following ones: %0 = insertvalue { i8, i32 } undef, i8 x, 0 %1 = insertvalue { i8, i32 } %0, i8 y, 0 Here the second instruction inserts values at the same indices, as the first one, making the first one redundant. It should be transformed to: %0 = insertvalue { i8, i32 } undef, i8 y, 0

Definition at line 500 of file InstCombineVectorOps.cpp.

500  {
501  bool IsRedundant = false;
502  ArrayRef<unsigned int> FirstIndices = I.getIndices();
503 
504  // If there is a chain of insertvalue instructions (each of them except the
505  // last one has only one use and it's another insertvalue insn from this
506  // chain), check if any of the 'children' uses the same indices as the first
507  // instruction. In this case, the first one is redundant.
508  Value *V = &I;
509  unsigned Depth = 0;
510  while (V->hasOneUse() && Depth < 10) {
511  User *U = V->user_back();
512  auto UserInsInst = dyn_cast<InsertValueInst>(U);
513  if (!UserInsInst || U->getOperand(0) != V)
514  break;
515  if (UserInsInst->getIndices() == FirstIndices) {
516  IsRedundant = true;
517  break;
518  }
519  V = UserInsInst;
520  Depth++;
521  }
522 
523  if (IsRedundant)
524  return ReplaceInstUsesWith(I, I.getOperand(0));
525  return nullptr;
526 }
ArrayRef< unsigned > getIndices() const
Instruction * ReplaceInstUsesWith(Instruction &I, Value *V)
Definition: InstCombine.h:282
Value * getOperand(unsigned i) const
Definition: User.h:90
LLVM_ATTRIBUTE_UNUSED_RESULT std::enable_if< !is_simple_type< Y >::value, typename cast_retty< X, const Y >::ret_type >::type dyn_cast(const Y &Val)
Definition: Casting.h:265
#define I(x, y, z)
Definition: MD5.cpp:54
bool hasOneUse() const
Definition: Value.h:284
LLVM Value Representation.
Definition: Value.h:69
User * user_back()
Definition: Value.h:271
Instruction* llvm::InstCombiner::visitInstruction ( Instruction I)
inline

Definition at line 224 of file InstCombine.h.

224 { return nullptr; }
Instruction * llvm::InstVisitor< InstCombiner , Instruction * >::visitIntrinsicInst ( IntrinsicInst I)
inlineinherited

Definition at line 216 of file InstVisitor.h.

216 { DELEGATE(CallInst); }
#define DELEGATE(CLASS_TO_VISIT)
Definition: InstVisitor.h:30
Instruction * InstCombiner::visitIntToPtr ( IntToPtrInst CI)

Definition at line 1406 of file InstCombineCasts.cpp.

1406  {
1407  // If the source integer type is not the intptr_t type for this target, do a
1408  // trunc or zext to the intptr_t type, then inttoptr of it. This allows the
1409  // cast to be exposed to other transforms.
1410 
1411  if (DL) {
1412  unsigned AS = CI.getAddressSpace();
1413  if (CI.getOperand(0)->getType()->getScalarSizeInBits() !=
1414  DL->getPointerSizeInBits(AS)) {
1415  Type *Ty = DL->getIntPtrType(CI.getContext(), AS);
1416  if (CI.getType()->isVectorTy()) // Handle vectors of pointers.
1417  Ty = VectorType::get(Ty, CI.getType()->getVectorNumElements());
1418 
1419  Value *P = Builder->CreateZExtOrTrunc(CI.getOperand(0), Ty);
1420  return new IntToPtrInst(P, CI.getType());
1421  }
1422  }
1423 
1424  if (Instruction *I = commonCastTransforms(CI))
1425  return I;
1426 
1427  return nullptr;
1428 }
Instruction * commonCastTransforms(CastInst &CI)
Implement the transforms common to all CastInst visitors.
#define P(N)
bool isVectorTy() const
Definition: Type.h:226
Value * getOperand(unsigned i) const
Definition: User.h:90
This class represents a cast from an integer to a pointer.
BuilderTy * Builder
Definition: InstCombine.h:102
LLVMContext & getContext() const
All values hold a context through their type.
Definition: Value.cpp:615
IntegerType * getIntPtrType(LLVMContext &C, unsigned AddressSpace=0) const
Definition: DataLayout.cpp:703
unsigned getVectorNumElements() const
Definition: Type.cpp:208
unsigned getScalarSizeInBits() const LLVM_READONLY
Definition: Type.cpp:135
Type * getType() const
Definition: Value.h:215
unsigned getAddressSpace() const
Returns the address space of this instruction's pointer type.
Value * CreateZExtOrTrunc(Value *V, Type *DestTy, const Twine &Name="")
Create a ZExt or Trunc from the integer value V to DestTy. Return the value untouched if the type of ...
Definition: IRBuilder.h:1105
#define I(x, y, z)
Definition: MD5.cpp:54
unsigned getPointerSizeInBits(unsigned AS=0) const
Definition: DataLayout.h:313
const DataLayout * DL
Definition: InstCombine.h:89
LLVM Value Representation.
Definition: Value.h:69
static VectorType * get(Type *ElementType, unsigned NumElements)
Definition: Type.cpp:703
Instruction * llvm::InstVisitor< InstCombiner , Instruction * >::visitIntToPtrInst ( IntToPtrInst I)
inlineinherited

Definition at line 192 of file InstVisitor.h.

192 { DELEGATE(CastInst);}
#define DELEGATE(CLASS_TO_VISIT)
Definition: InstVisitor.h:30
Instruction * InstCombiner::visitInvokeInst ( InvokeInst II)

Definition at line 1004 of file InstCombineCalls.cpp.

1004  {
1005  return visitCallSite(&II);
1006 }
Instruction * visitCallSite(CallSite CS)
Instruction * InstCombiner::visitLandingPadInst ( LandingPadInst LI)

Definition at line 2193 of file InstructionCombining.cpp.

2193  {
2194  // The logic here should be correct for any real-world personality function.
2195  // However if that turns out not to be true, the offending logic can always
2196  // be conditioned on the personality function, like the catch-all logic is.
2198 
2199  // Simplify the list of clauses, eg by removing repeated catch clauses
2200  // (these are often created by inlining).
2201  bool MakeNewInstruction = false; // If true, recreate using the following:
2202  SmallVector<Constant *, 16> NewClauses; // - Clauses for the new instruction;
2203  bool CleanupFlag = LI.isCleanup(); // - The new instruction is a cleanup.
2204 
2205  SmallPtrSet<Value *, 16> AlreadyCaught; // Typeinfos known caught already.
2206  for (unsigned i = 0, e = LI.getNumClauses(); i != e; ++i) {
2207  bool isLastClause = i + 1 == e;
2208  if (LI.isCatch(i)) {
2209  // A catch clause.
2210  Constant *CatchClause = LI.getClause(i);
2211  Constant *TypeInfo = CatchClause->stripPointerCasts();
2212 
2213  // If we already saw this clause, there is no point in having a second
2214  // copy of it.
2215  if (AlreadyCaught.insert(TypeInfo)) {
2216  // This catch clause was not already seen.
2217  NewClauses.push_back(CatchClause);
2218  } else {
2219  // Repeated catch clause - drop the redundant copy.
2220  MakeNewInstruction = true;
2221  }
2222 
2223  // If this is a catch-all then there is no point in keeping any following
2224  // clauses or marking the landingpad as having a cleanup.
2225  if (isCatchAll(Personality, TypeInfo)) {
2226  if (!isLastClause)
2227  MakeNewInstruction = true;
2228  CleanupFlag = false;
2229  break;
2230  }
2231  } else {
2232  // A filter clause. If any of the filter elements were already caught
2233  // then they can be dropped from the filter. It is tempting to try to
2234  // exploit the filter further by saying that any typeinfo that does not
2235  // occur in the filter can't be caught later (and thus can be dropped).
2236  // However this would be wrong, since typeinfos can match without being
2237  // equal (for example if one represents a C++ class, and the other some
2238  // class derived from it).
2239  assert(LI.isFilter(i) && "Unsupported landingpad clause!");
2240  Constant *FilterClause = LI.getClause(i);
2241  ArrayType *FilterType = cast<ArrayType>(FilterClause->getType());
2242  unsigned NumTypeInfos = FilterType->getNumElements();
2243 
2244  // An empty filter catches everything, so there is no point in keeping any
2245  // following clauses or marking the landingpad as having a cleanup. By
2246  // dealing with this case here the following code is made a bit simpler.
2247  if (!NumTypeInfos) {
2248  NewClauses.push_back(FilterClause);
2249  if (!isLastClause)
2250  MakeNewInstruction = true;
2251  CleanupFlag = false;
2252  break;
2253  }
2254 
2255  bool MakeNewFilter = false; // If true, make a new filter.
2256  SmallVector<Constant *, 16> NewFilterElts; // New elements.
2257  if (isa<ConstantAggregateZero>(FilterClause)) {
2258  // Not an empty filter - it contains at least one null typeinfo.
2259  assert(NumTypeInfos > 0 && "Should have handled empty filter already!");
2260  Constant *TypeInfo =
2261  Constant::getNullValue(FilterType->getElementType());
2262  // If this typeinfo is a catch-all then the filter can never match.
2263  if (isCatchAll(Personality, TypeInfo)) {
2264  // Throw the filter away.
2265  MakeNewInstruction = true;
2266  continue;
2267  }
2268 
2269  // There is no point in having multiple copies of this typeinfo, so
2270  // discard all but the first copy if there is more than one.
2271  NewFilterElts.push_back(TypeInfo);
2272  if (NumTypeInfos > 1)
2273  MakeNewFilter = true;
2274  } else {
2275  ConstantArray *Filter = cast<ConstantArray>(FilterClause);
2276  SmallPtrSet<Value *, 16> SeenInFilter; // For uniquing the elements.
2277  NewFilterElts.reserve(NumTypeInfos);
2278 
2279  // Remove any filter elements that were already caught or that already
2280  // occurred in the filter. While there, see if any of the elements are
2281  // catch-alls. If so, the filter can be discarded.
2282  bool SawCatchAll = false;
2283  for (unsigned j = 0; j != NumTypeInfos; ++j) {
2284  Constant *Elt = Filter->getOperand(j);
2285  Constant *TypeInfo = Elt->stripPointerCasts();
2286  if (isCatchAll(Personality, TypeInfo)) {
2287  // This element is a catch-all. Bail out, noting this fact.
2288  SawCatchAll = true;
2289  break;
2290  }
2291  if (AlreadyCaught.count(TypeInfo))
2292  // Already caught by an earlier clause, so having it in the filter
2293  // is pointless.
2294  continue;
2295  // There is no point in having multiple copies of the same typeinfo in
2296  // a filter, so only add it if we didn't already.
2297  if (SeenInFilter.insert(TypeInfo))
2298  NewFilterElts.push_back(cast<Constant>(Elt));
2299  }
2300  // A filter containing a catch-all cannot match anything by definition.
2301  if (SawCatchAll) {
2302  // Throw the filter away.
2303  MakeNewInstruction = true;
2304  continue;
2305  }
2306 
2307  // If we dropped something from the filter, make a new one.
2308  if (NewFilterElts.size() < NumTypeInfos)
2309  MakeNewFilter = true;
2310  }
2311  if (MakeNewFilter) {
2312  FilterType = ArrayType::get(FilterType->getElementType(),
2313  NewFilterElts.size());
2314  FilterClause = ConstantArray::get(FilterType, NewFilterElts);
2315  MakeNewInstruction = true;
2316  }
2317 
2318  NewClauses.push_back(FilterClause);
2319 
2320  // If the new filter is empty then it will catch everything so there is
2321  // no point in keeping any following clauses or marking the landingpad
2322  // as having a cleanup. The case of the original filter being empty was
2323  // already handled above.
2324  if (MakeNewFilter && !NewFilterElts.size()) {
2325  assert(MakeNewInstruction && "New filter but not a new instruction!");
2326  CleanupFlag = false;
2327  break;
2328  }
2329  }
2330  }
2331 
2332  // If several filters occur in a row then reorder them so that the shortest
2333  // filters come first (those with the smallest number of elements). This is
2334  // advantageous because shorter filters are more likely to match, speeding up
2335  // unwinding, but mostly because it increases the effectiveness of the other
2336  // filter optimizations below.
2337  for (unsigned i = 0, e = NewClauses.size(); i + 1 < e; ) {
2338  unsigned j;
2339  // Find the maximal 'j' s.t. the range [i, j) consists entirely of filters.
2340  for (j = i; j != e; ++j)
2341  if (!isa<ArrayType>(NewClauses[j]->getType()))
2342  break;
2343 
2344  // Check whether the filters are already sorted by length. We need to know
2345  // if sorting them is actually going to do anything so that we only make a
2346  // new landingpad instruction if it does.
2347  for (unsigned k = i; k + 1 < j; ++k)
2348  if (shorter_filter(NewClauses[k+1], NewClauses[k])) {
2349  // Not sorted, so sort the filters now. Doing an unstable sort would be
2350  // correct too but reordering filters pointlessly might confuse users.
2351  std::stable_sort(NewClauses.begin() + i, NewClauses.begin() + j,
2352  shorter_filter);
2353  MakeNewInstruction = true;
2354  break;
2355  }
2356 
2357  // Look for the next batch of filters.
2358  i = j + 1;
2359  }
2360 
2361  // If typeinfos matched if and only if equal, then the elements of a filter L
2362  // that occurs later than a filter F could be replaced by the intersection of
2363  // the elements of F and L. In reality two typeinfos can match without being
2364  // equal (for example if one represents a C++ class, and the other some class
2365  // derived from it) so it would be wrong to perform this transform in general.
2366  // However the transform is correct and useful if F is a subset of L. In that
2367  // case L can be replaced by F, and thus removed altogether since repeating a
2368  // filter is pointless. So here we look at all pairs of filters F and L where
2369  // L follows F in the list of clauses, and remove L if every element of F is
2370  // an element of L. This can occur when inlining C++ functions with exception
2371  // specifications.
2372  for (unsigned i = 0; i + 1 < NewClauses.size(); ++i) {
2373  // Examine each filter in turn.
2374  Value *Filter = NewClauses[i];
2375  ArrayType *FTy = dyn_cast<ArrayType>(Filter->getType());
2376  if (!FTy)
2377  // Not a filter - skip it.
2378  continue;
2379  unsigned FElts = FTy->getNumElements();
2380  // Examine each filter following this one. Doing this backwards means that
2381  // we don't have to worry about filters disappearing under us when removed.
2382  for (unsigned j = NewClauses.size() - 1; j != i; --j) {
2383  Value *LFilter = NewClauses[j];
2384  ArrayType *LTy = dyn_cast<ArrayType>(LFilter->getType());
2385  if (!LTy)
2386  // Not a filter - skip it.
2387  continue;
2388  // If Filter is a subset of LFilter, i.e. every element of Filter is also
2389  // an element of LFilter, then discard LFilter.
2390  SmallVectorImpl<Constant *>::iterator J = NewClauses.begin() + j;
2391  // If Filter is empty then it is a subset of LFilter.
2392  if (!FElts) {
2393  // Discard LFilter.
2394  NewClauses.erase(J);
2395  MakeNewInstruction = true;
2396  // Move on to the next filter.
2397  continue;
2398  }
2399  unsigned LElts = LTy->getNumElements();
2400  // If Filter is longer than LFilter then it cannot be a subset of it.
2401  if (FElts > LElts)
2402  // Move on to the next filter.
2403  continue;
2404  // At this point we know that LFilter has at least one element.
2405  if (isa<ConstantAggregateZero>(LFilter)) { // LFilter only contains zeros.
2406  // Filter is a subset of LFilter iff Filter contains only zeros (as we
2407  // already know that Filter is not longer than LFilter).
2408  if (isa<ConstantAggregateZero>(Filter)) {
2409  assert(FElts <= LElts && "Should have handled this case earlier!");
2410  // Discard LFilter.
2411  NewClauses.erase(J);
2412  MakeNewInstruction = true;
2413  }
2414  // Move on to the next filter.
2415  continue;
2416  }
2417  ConstantArray *LArray = cast<ConstantArray>(LFilter);
2418  if (isa<ConstantAggregateZero>(Filter)) { // Filter only contains zeros.
2419  // Since Filter is non-empty and contains only zeros, it is a subset of
2420  // LFilter iff LFilter contains a zero.
2421  assert(FElts > 0 && "Should have eliminated the empty filter earlier!");
2422  for (unsigned l = 0; l != LElts; ++l)
2423  if (LArray->getOperand(l)->isNullValue()) {
2424  // LFilter contains a zero - discard it.
2425  NewClauses.erase(J);
2426  MakeNewInstruction = true;
2427  break;
2428  }
2429  // Move on to the next filter.
2430  continue;
2431  }
2432  // At this point we know that both filters are ConstantArrays. Loop over
2433  // operands to see whether every element of Filter is also an element of
2434  // LFilter. Since filters tend to be short this is probably faster than
2435  // using a method that scales nicely.
2436  ConstantArray *FArray = cast<ConstantArray>(Filter);
2437  bool AllFound = true;
2438  for (unsigned f = 0; f != FElts; ++f) {
2439  Value *FTypeInfo = FArray->getOperand(f)->stripPointerCasts();
2440  AllFound = false;
2441  for (unsigned l = 0; l != LElts; ++l) {
2442  Value *LTypeInfo = LArray->getOperand(l)->stripPointerCasts();
2443  if (LTypeInfo == FTypeInfo) {
2444  AllFound = true;
2445  break;
2446  }
2447  }
2448  if (!AllFound)
2449  break;
2450  }
2451  if (AllFound) {
2452  // Discard LFilter.
2453  NewClauses.erase(J);
2454  MakeNewInstruction = true;
2455  }
2456  // Move on to the next filter.
2457  }
2458  }
2459 
2460  // If we changed any of the clauses, replace the old landingpad instruction
2461  // with a new one.
2462  if (MakeNewInstruction) {
2464  LI.getPersonalityFn(),
2465  NewClauses.size());
2466  for (unsigned i = 0, e = NewClauses.size(); i != e; ++i)
2467  NLI->addClause(NewClauses[i]);
2468  // A landing pad with no clauses must have the cleanup flag set. It is
2469  // theoretically possible, though highly unlikely, that we eliminated all
2470  // clauses. If so, force the cleanup flag to true.
2471  if (NewClauses.empty())
2472  CleanupFlag = true;
2473  NLI->setCleanup(CleanupFlag);
2474  return NLI;
2475  }
2476 
2477  // Even if none of the clauses changed, we may nonetheless have understood
2478  // that the cleanup flag is pointless. Clear it if so.
2479  if (LI.isCleanup() != CleanupFlag) {
2480  assert(!CleanupFlag && "Adding a cleanup, not removing one?!");
2481  LI.setCleanup(CleanupFlag);
2482  return &LI;
2483  }
2484 
2485  return nullptr;
2486 }
SuperClass::iterator iterator
Definition: SmallVector.h:351
void reserve(unsigned N)
Definition: SmallVector.h:401
Constant * getClause(unsigned Idx) const
size_type count(PtrType Ptr) const
count - Return 1 if the specified pointer is in the set, 0 otherwise.
Definition: SmallPtrSet.h:267
bool insert(PtrType Ptr)
Definition: SmallPtrSet.h:256
FunctionType * getType(LLVMContext &Context, ID id, ArrayRef< Type * > Tys=None)
Definition: Function.cpp:711
Value * getPersonalityFn() const
LoopInfoBase< BlockT, LoopT > * LI
Definition: LoopInfoImpl.h:412
static Constant * getNullValue(Type *Ty)
Definition: Constants.cpp:133
static bool shorter_filter(const Value *LHS, const Value *RHS)
void setCleanup(bool V)
setCleanup - Indicate that this landingpad instruction is a cleanup.
bool LLVM_ATTRIBUTE_UNUSED_RESULT empty() const
Definition: SmallVector.h:57
foo f
unsigned getNumClauses() const
getNumClauses - Get the number of clauses for this landing pad.
Type * getElementType() const
Definition: DerivedTypes.h:319
bool isFilter(unsigned Idx) const
isFilter - Return 'true' if the clause and index Idx is a filter clause.
static bool isCatchAll(Personality_Type Personality, Constant *TypeInfo)
isCatchAll - Return 'true' if the given typeinfo will match anything.
assert(Globals.size() > 1)
Constant * stripPointerCasts()
Definition: Constant.h:173
LLVM Constant Representation.
Definition: Constant.h:41
static Constant * get(ArrayType *T, ArrayRef< Constant * > V)
Definition: Constants.cpp:783
uint64_t getNumElements() const
Definition: DerivedTypes.h:348
void addClause(Constant *ClauseVal)
Add a catch or filter clause to the landing pad.
Value * getOperand(unsigned i) const
Definition: User.h:90
static LandingPadInst * Create(Type *RetTy, Value *PersonalityFn, unsigned NumReservedClauses, const Twine &NameStr="", Instruction *InsertBefore=nullptr)
iterator erase(iterator I)
Definition: SmallVector.h:450
Type * getType() const
Definition: Value.h:215
Value * stripPointerCasts()
Strips off any unneeded pointer casts, all-zero GEPs and aliases from the specified value...
Definition: Value.cpp:423
LLVM_ATTRIBUTE_UNUSED_RESULT std::enable_if< !is_simple_type< Y >::value, typename cast_retty< X, const Y >::ret_type >::type dyn_cast(const Y &Val)
Definition: Casting.h:265
bool isCatch(unsigned Idx) const
isCatch - Return 'true' if the clause and index Idx is a catch clause.
static ArrayType * get(Type *ElementType, uint64_t NumElements)
Definition: Type.cpp:676
LLVM Value Representation.
Definition: Value.h:69
bool isCleanup() const
static Personality_Type RecognizePersonality(Value *Pers)
Instruction * InstCombiner::visitLoadInst ( LoadInst LI)

Definition at line 360 of file InstCombineLoadStoreAlloca.cpp.

360  {
361  Value *Op = LI.getOperand(0);
362 
363  // Attempt to improve the alignment.
364  if (DL) {
365  unsigned KnownAlign =
367  unsigned LoadAlign = LI.getAlignment();
368  unsigned EffectiveLoadAlign = LoadAlign != 0 ? LoadAlign :
370 
371  if (KnownAlign > EffectiveLoadAlign)
372  LI.setAlignment(KnownAlign);
373  else if (LoadAlign == 0)
374  LI.setAlignment(EffectiveLoadAlign);
375  }
376 
377  // load (cast X) --> cast (load X) iff safe.
378  if (isa<CastInst>(Op))
379  if (Instruction *Res = InstCombineLoadCast(*this, LI, DL))
380  return Res;
381 
382  // None of the following transforms are legal for volatile/atomic loads.
383  // FIXME: Some of it is okay for atomic loads; needs refactoring.
384  if (!LI.isSimple()) return nullptr;
385 
386  // Do really simple store-to-load forwarding and load CSE, to catch cases
387  // where there are several consecutive memory accesses to the same location,
388  // separated by a few arithmetic operations.
389  BasicBlock::iterator BBI = &LI;
390  if (Value *AvailableVal = FindAvailableLoadedValue(Op, LI.getParent(), BBI,6))
391  return ReplaceInstUsesWith(LI, AvailableVal);
392 
393  // load(gep null, ...) -> unreachable
394  if (GetElementPtrInst *GEPI = dyn_cast<GetElementPtrInst>(Op)) {
395  const Value *GEPI0 = GEPI->getOperand(0);
396  // TODO: Consider a target hook for valid address spaces for this xform.
397  if (isa<ConstantPointerNull>(GEPI0) && GEPI->getPointerAddressSpace() == 0){
398  // Insert a new store to null instruction before the load to indicate
399  // that this code is not reachable. We do this instead of inserting
400  // an unreachable instruction directly because we cannot modify the
401  // CFG.
403  Constant::getNullValue(Op->getType()), &LI);
404  return ReplaceInstUsesWith(LI, UndefValue::get(LI.getType()));
405  }
406  }
407 
408  // load null/undef -> unreachable
409  // TODO: Consider a target hook for valid address spaces for this xform.
410  if (isa<UndefValue>(Op) ||
411  (isa<ConstantPointerNull>(Op) && LI.getPointerAddressSpace() == 0)) {
412  // Insert a new store to null instruction before the load to indicate that
413  // this code is not reachable. We do this instead of inserting an
414  // unreachable instruction directly because we cannot modify the CFG.
416  Constant::getNullValue(Op->getType()), &LI);
417  return ReplaceInstUsesWith(LI, UndefValue::get(LI.getType()));
418  }
419 
420  // Instcombine load (constantexpr_cast global) -> cast (load global)
421  if (ConstantExpr *CE = dyn_cast<ConstantExpr>(Op))
422  if (CE->isCast())
423  if (Instruction *Res = InstCombineLoadCast(*this, LI, DL))
424  return Res;
425 
426  if (Op->hasOneUse()) {
427  // Change select and PHI nodes to select values instead of addresses: this
428  // helps alias analysis out a lot, allows many others simplifications, and
429  // exposes redundancy in the code.
430  //
431  // Note that we cannot do the transformation unless we know that the
432  // introduced loads cannot trap! Something like this is valid as long as
433  // the condition is always false: load (select bool %C, int* null, int* %G),
434  // but it would not be valid if we transformed it to load from null
435  // unconditionally.
436  //
437  if (SelectInst *SI = dyn_cast<SelectInst>(Op)) {
438  // load (select (Cond, &V1, &V2)) --> select(Cond, load &V1, load &V2).
439  unsigned Align = LI.getAlignment();
440  if (isSafeToLoadUnconditionally(SI->getOperand(1), SI, Align, DL) &&
441  isSafeToLoadUnconditionally(SI->getOperand(2), SI, Align, DL)) {
442  LoadInst *V1 = Builder->CreateLoad(SI->getOperand(1),
443  SI->getOperand(1)->getName()+".val");
444  LoadInst *V2 = Builder->CreateLoad(SI->getOperand(2),
445  SI->getOperand(2)->getName()+".val");
446  V1->setAlignment(Align);
447  V2->setAlignment(Align);
448  return SelectInst::Create(SI->getCondition(), V1, V2);
449  }
450 
451  // load (select (cond, null, P)) -> load P
452  if (Constant *C = dyn_cast<Constant>(SI->getOperand(1)))
453  if (C->isNullValue()) {
454  LI.setOperand(0, SI->getOperand(2));
455  return &LI;
456  }
457 
458  // load (select (cond, P, null)) -> load P
459  if (Constant *C = dyn_cast<Constant>(SI->getOperand(2)))
460  if (C->isNullValue()) {
461  LI.setOperand(0, SI->getOperand(1));
462  return &LI;
463  }
464  }
465  }
466  return nullptr;
467 }
LoadInst * CreateLoad(Value *Ptr, const char *Name)
Definition: IRBuilder.h:901
unsigned getOrEnforceKnownAlignment(Value *V, unsigned PrefAlign, const DataLayout *TD=nullptr)
unsigned getPrefTypeAlignment(Type *Ty) const
Definition: DataLayout.cpp:693
bool isSimple() const
Definition: Instructions.h:235
LoopInfoBase< BlockT, LoopT > * LI
Definition: LoopInfoImpl.h:412
static Constant * getNullValue(Type *Ty)
Definition: Constants.cpp:133
bool isSafeToLoadUnconditionally(Value *V, Instruction *ScanFrom, unsigned Align, const DataLayout *TD=nullptr)
Definition: Loads.cpp:56
static SelectInst * Create(Value *C, Value *S1, Value *S2, const Twine &NameStr="", Instruction *InsertBefore=nullptr)
static Instruction * InstCombineLoadCast(InstCombiner &IC, LoadInst &LI, const DataLayout *DL)
InstCombineLoadCast - Fold 'load (cast P)' -> cast (load P)' when possible.
LLVM Constant Representation.
Definition: Constant.h:41
Instruction * ReplaceInstUsesWith(Instruction &I, Value *V)
Definition: InstCombine.h:282
Value * getOperand(unsigned i) const
Definition: User.h:90
void setAlignment(unsigned Align)
Value * FindAvailableLoadedValue(Value *Ptr, BasicBlock *ScanBB, BasicBlock::iterator &ScanFrom, unsigned MaxInstsToScan=6, AliasAnalysis *AA=nullptr, MDNode **TBAATag=nullptr)
Definition: Loads.cpp:139
BuilderTy * Builder
Definition: InstCombine.h:102
static UndefValue * get(Type *T)
Definition: Constants.cpp:1372
unsigned getABITypeAlignment(Type *Ty) const
Definition: DataLayout.cpp:683
Type * getType() const
Definition: Value.h:215
unsigned getPointerAddressSpace() const
Returns the address space of the pointer operand.
Definition: Instructions.h:245
static cl::opt< AlignMode > Align(cl::desc("Load/store alignment support"), cl::Hidden, cl::init(NoStrictAlign), cl::values(clEnumValN(StrictAlign,"aarch64-strict-align","Disallow all unaligned memory accesses"), clEnumValN(NoStrictAlign,"aarch64-no-strict-align","Allow unaligned memory accesses"), clEnumValEnd))
void setOperand(unsigned i, Value *Val)
Definition: User.h:94
unsigned getAlignment() const
Definition: Instructions.h:198
bool hasOneUse() const
Definition: Value.h:284
const DataLayout * DL
Definition: InstCombine.h:89
LLVM Value Representation.
Definition: Value.h:69
const BasicBlock * getParent() const
Definition: Instruction.h:53
Instruction * InstCombiner::visitLShr ( BinaryOperator I)

Definition at line 729 of file InstCombineShifts.cpp.

729  {
730  if (Value *V = SimplifyVectorOp(I))
731  return ReplaceInstUsesWith(I, V);
732 
733  if (Value *V = SimplifyLShrInst(I.getOperand(0), I.getOperand(1),
734  I.isExact(), DL))
735  return ReplaceInstUsesWith(I, V);
736 
738  return R;
739 
740  Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
741 
742  if (ConstantInt *Op1C = dyn_cast<ConstantInt>(Op1)) {
743  unsigned ShAmt = Op1C->getZExtValue();
744 
745  if (IntrinsicInst *II = dyn_cast<IntrinsicInst>(Op0)) {
746  unsigned BitWidth = Op0->getType()->getScalarSizeInBits();
747  // ctlz.i32(x)>>5 --> zext(x == 0)
748  // cttz.i32(x)>>5 --> zext(x == 0)
749  // ctpop.i32(x)>>5 --> zext(x == -1)
750  if ((II->getIntrinsicID() == Intrinsic::ctlz ||
751  II->getIntrinsicID() == Intrinsic::cttz ||
752  II->getIntrinsicID() == Intrinsic::ctpop) &&
753  isPowerOf2_32(BitWidth) && Log2_32(BitWidth) == ShAmt) {
754  bool isCtPop = II->getIntrinsicID() == Intrinsic::ctpop;
755  Constant *RHS = ConstantInt::getSigned(Op0->getType(), isCtPop ? -1:0);
756  Value *Cmp = Builder->CreateICmpEQ(II->getArgOperand(0), RHS);
757  return new ZExtInst(Cmp, II->getType());
758  }
759  }
760 
761  // If the shifted-out value is known-zero, then this is an exact shift.
762  if (!I.isExact() &&
763  MaskedValueIsZero(Op0,APInt::getLowBitsSet(Op1C->getBitWidth(),ShAmt))){
764  I.setIsExact();
765  return &I;
766  }
767  }
768 
769  return nullptr;
770 }
This class represents zero extension of integer types.
Value * SimplifyVectorOp(BinaryOperator &Inst)
Makes transformation of binary operation specific for vector types.
static APInt getLowBitsSet(unsigned numBits, unsigned loBitsSet)
Get a value with low bits set.
Definition: APInt.h:526
Value * CreateICmpEQ(Value *LHS, Value *RHS, const Twine &Name="")
Definition: IRBuilder.h:1247
Instruction * commonShiftTransforms(BinaryOperator &I)
LLVM Constant Representation.
Definition: Constant.h:41
Instruction * ReplaceInstUsesWith(Instruction &I, Value *V)
Definition: InstCombine.h:282
Value * getOperand(unsigned i) const
Definition: User.h:90
BuilderTy * Builder
Definition: InstCombine.h:102
void setIsExact(bool b=true)
Class for constant integers.
Definition: Constants.h:51
unsigned getScalarSizeInBits() const LLVM_READONLY
Definition: Type.cpp:135
Type * getType() const
Definition: Value.h:215
static ConstantInt * getSigned(IntegerType *Ty, int64_t V)
Get a ConstantInt for a specific signed value.
Definition: Constants.cpp:543
bool isExact() const
isExact - Determine whether the exact flag is set.
unsigned Log2_32(uint32_t Value)
Definition: MathExtras.h:452
Value * SimplifyLShrInst(Value *Op0, Value *Op1, bool isExact, const DataLayout *TD=nullptr, const TargetLibraryInfo *TLI=nullptr, const DominatorTree *DT=nullptr)
#define I(x, y, z)
Definition: MD5.cpp:54
bool MaskedValueIsZero(Value *V, const APInt &Mask, unsigned Depth=0) const
Definition: InstCombine.h:323
const DataLayout * DL
Definition: InstCombine.h:89
LLVM Value Representation.
Definition: Value.h:69
bool isPowerOf2_32(uint32_t Value)
Definition: MathExtras.h:363
Instruction * llvm::InstVisitor< InstCombiner , Instruction * >::visitMemCpyInst ( MemCpyInst I)
inlineinherited

Definition at line 209 of file InstVisitor.h.

209 { DELEGATE(MemTransferInst); }
#define DELEGATE(CLASS_TO_VISIT)
Definition: InstVisitor.h:30
Instruction * llvm::InstVisitor< InstCombiner , Instruction * >::visitMemIntrinsic ( MemIntrinsic I)
inlineinherited

Definition at line 212 of file InstVisitor.h.

212 { DELEGATE(IntrinsicInst); }
#define DELEGATE(CLASS_TO_VISIT)
Definition: InstVisitor.h:30
Instruction * llvm::InstVisitor< InstCombiner , Instruction * >::visitMemMoveInst ( MemMoveInst I)
inlineinherited

Definition at line 210 of file InstVisitor.h.

210 { DELEGATE(MemTransferInst); }
#define DELEGATE(CLASS_TO_VISIT)
Definition: InstVisitor.h:30
Instruction * llvm::InstVisitor< InstCombiner , Instruction * >::visitMemSetInst ( MemSetInst I)
inlineinherited

Definition at line 208 of file InstVisitor.h.

208 { DELEGATE(MemIntrinsic); }
#define DELEGATE(CLASS_TO_VISIT)
Definition: InstVisitor.h:30
Instruction * llvm::InstVisitor< InstCombiner , Instruction * >::visitMemTransferInst ( MemTransferInst I)
inlineinherited

Definition at line 211 of file InstVisitor.h.

211 { DELEGATE(MemIntrinsic); }
#define DELEGATE(CLASS_TO_VISIT)
Definition: InstVisitor.h:30
void llvm::InstVisitor< InstCombiner , Instruction * >::visitModule ( Module M)
inlineinherited

Definition at line 141 of file InstVisitor.h.

141 {}
Instruction * InstCombiner::visitMul ( BinaryOperator I)

i1 mul -> i1 and.

Definition at line 119 of file InstCombineMulDivRem.cpp.

119  {
120  bool Changed = SimplifyAssociativeOrCommutative(I);
121  Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
122 
123  if (Value *V = SimplifyVectorOp(I))
124  return ReplaceInstUsesWith(I, V);
125 
126  if (Value *V = SimplifyMulInst(Op0, Op1, DL))
127  return ReplaceInstUsesWith(I, V);
128 
130  return ReplaceInstUsesWith(I, V);
131 
132  if (match(Op1, m_AllOnes())) // X * -1 == 0 - X
133  return BinaryOperator::CreateNeg(Op0, I.getName());
134 
135  // Also allow combining multiply instructions on vectors.
136  {
137  Value *NewOp;
138  Constant *C1, *C2;
139  const APInt *IVal;
140  if (match(&I, m_Mul(m_Shl(m_Value(NewOp), m_Constant(C2)),
141  m_Constant(C1))) &&
142  match(C1, m_APInt(IVal)))
143  // ((X << C1)*C2) == (X * (C2 << C1))
144  return BinaryOperator::CreateMul(NewOp, ConstantExpr::getShl(C1, C2));
145 
146  if (match(&I, m_Mul(m_Value(NewOp), m_Constant(C1)))) {
147  Constant *NewCst = nullptr;
148  if (match(C1, m_APInt(IVal)) && IVal->isPowerOf2())
149  // Replace X*(2^C) with X << C, where C is either a scalar or a splat.
150  NewCst = ConstantInt::get(NewOp->getType(), IVal->logBase2());
151  else if (ConstantDataVector *CV = dyn_cast<ConstantDataVector>(C1))
152  // Replace X*(2^C) with X << C, where C is a vector of known
153  // constant powers of 2.
154  NewCst = getLogBase2Vector(CV);
155 
156  if (NewCst) {
157  BinaryOperator *Shl = BinaryOperator::CreateShl(NewOp, NewCst);
158  if (I.hasNoSignedWrap()) Shl->setHasNoSignedWrap();
159  if (I.hasNoUnsignedWrap()) Shl->setHasNoUnsignedWrap();
160  return Shl;
161  }
162  }
163  }
164 
165  if (ConstantInt *CI = dyn_cast<ConstantInt>(Op1)) {
166  // (Y - X) * (-(2**n)) -> (X - Y) * (2**n), for positive nonzero n
167  // (Y + const) * (-(2**n)) -> (-constY) * (2**n), for positive nonzero n
168  // The "* (2**n)" thus becomes a potential shifting opportunity.
169  {
170  const APInt & Val = CI->getValue();
171  const APInt &PosVal = Val.abs();
172  if (Val.isNegative() && PosVal.isPowerOf2()) {
173  Value *X = nullptr, *Y = nullptr;
174  if (Op0->hasOneUse()) {
175  ConstantInt *C1;
176  Value *Sub = nullptr;
177  if (match(Op0, m_Sub(m_Value(Y), m_Value(X))))
178  Sub = Builder->CreateSub(X, Y, "suba");
179  else if (match(Op0, m_Add(m_Value(Y), m_ConstantInt(C1))))
180  Sub = Builder->CreateSub(Builder->CreateNeg(C1), Y, "subc");
181  if (Sub)
182  return
183  BinaryOperator::CreateMul(Sub,
184  ConstantInt::get(Y->getType(), PosVal));
185  }
186  }
187  }
188  }
189 
190  // Simplify mul instructions with a constant RHS.
191  if (isa<Constant>(Op1)) {
192  // Try to fold constant mul into select arguments.
193  if (SelectInst *SI = dyn_cast<SelectInst>(Op0))
194  if (Instruction *R = FoldOpIntoSelect(I, SI))
195  return R;
196 
197  if (isa<PHINode>(Op0))
198  if (Instruction *NV = FoldOpIntoPhi(I))
199  return NV;
200 
201  // Canonicalize (X+C1)*CI -> X*CI+C1*CI.
202  {
203  Value *X;
204  Constant *C1;
205  if (match(Op0, m_OneUse(m_Add(m_Value(X), m_Constant(C1))))) {
206  Value *Mul = Builder->CreateMul(C1, Op1);
207  // Only go forward with the transform if C1*CI simplifies to a tidier
208  // constant.
209  if (!match(Mul, m_Mul(m_Value(), m_Value())))
210  return BinaryOperator::CreateAdd(Builder->CreateMul(X, Op1), Mul);
211  }
212  }
213  }
214 
215  if (Value *Op0v = dyn_castNegVal(Op0)) // -X * -Y = X*Y
216  if (Value *Op1v = dyn_castNegVal(Op1))
217  return BinaryOperator::CreateMul(Op0v, Op1v);
218 
219  // (X / Y) * Y = X - (X % Y)
220  // (X / Y) * -Y = (X % Y) - X
221  {
222  Value *Op1C = Op1;
224  if (!BO ||
225  (BO->getOpcode() != Instruction::UDiv &&
226  BO->getOpcode() != Instruction::SDiv)) {
227  Op1C = Op0;
228  BO = dyn_cast<BinaryOperator>(Op1);
229  }
230  Value *Neg = dyn_castNegVal(Op1C);
231  if (BO && BO->hasOneUse() &&
232  (BO->getOperand(1) == Op1C || BO->getOperand(1) == Neg) &&
233  (BO->getOpcode() == Instruction::UDiv ||
234  BO->getOpcode() == Instruction::SDiv)) {
235  Value *Op0BO = BO->getOperand(0), *Op1BO = BO->getOperand(1);
236 
237  // If the division is exact, X % Y is zero, so we end up with X or -X.
238  if (PossiblyExactOperator *SDiv = dyn_cast<PossiblyExactOperator>(BO))
239  if (SDiv->isExact()) {
240  if (Op1BO == Op1C)
241  return ReplaceInstUsesWith(I, Op0BO);
242  return BinaryOperator::CreateNeg(Op0BO);
243  }
244 
245  Value *Rem;
246  if (BO->getOpcode() == Instruction::UDiv)
247  Rem = Builder->CreateURem(Op0BO, Op1BO);
248  else
249  Rem = Builder->CreateSRem(Op0BO, Op1BO);
250  Rem->takeName(BO);
251 
252  if (Op1BO == Op1C)
253  return BinaryOperator::CreateSub(Op0BO, Rem);
254  return BinaryOperator::CreateSub(Rem, Op0BO);
255  }
256  }
257 
258  /// i1 mul -> i1 and.
259  if (I.getType()->getScalarType()->isIntegerTy(1))
260  return BinaryOperator::CreateAnd(Op0, Op1);
261 
262  // X*(1 << Y) --> X << Y
263  // (1 << Y)*X --> X << Y
264  {
265  Value *Y;
266  if (match(Op0, m_Shl(m_One(), m_Value(Y))))
267  return BinaryOperator::CreateShl(Op1, Y);
268  if (match(Op1, m_Shl(m_One(), m_Value(Y))))
269  return BinaryOperator::CreateShl(Op0, Y);
270  }
271 
272  // If one of the operands of the multiply is a cast from a boolean value, then
273  // we know the bool is either zero or one, so this is a 'masking' multiply.
274  // X * Y (where Y is 0 or 1) -> X & (0-Y)
275  if (!I.getType()->isVectorTy()) {
276  // -2 is "-1 << 1" so it is all bits set except the low one.
277  APInt Negative2(I.getType()->getPrimitiveSizeInBits(), (uint64_t)-2, true);
278 
279  Value *BoolCast = nullptr, *OtherOp = nullptr;
280  if (MaskedValueIsZero(Op0, Negative2))
281  BoolCast = Op0, OtherOp = Op1;
282  else if (MaskedValueIsZero(Op1, Negative2))
283  BoolCast = Op1, OtherOp = Op0;
284 
285  if (BoolCast) {
287  BoolCast);
288  return BinaryOperator::CreateAnd(V, OtherOp);
289  }
290  }
291 
292  return Changed ? &I : nullptr;
293 }
Instruction * FoldOpIntoPhi(Instruction &I)
class_match< Value > m_Value()
m_Value() - Match an arbitrary value and ignore it.
Definition: PatternMatch.h:70
void setHasNoSignedWrap(bool b=true)
APInt LLVM_ATTRIBUTE_UNUSED_RESULT abs() const
Get the absolute value;.
Definition: APInt.h:1546
BinaryOp_match< LHS, RHS, Instruction::Sub > m_Sub(const LHS &L, const RHS &R)
Definition: PatternMatch.h:407
static Constant * getLogBase2Vector(ConstantDataVector *CV)
A helper routine of InstCombiner::visitMul().
BinaryOp_match< LHS, RHS, Instruction::Mul > m_Mul(const LHS &L, const RHS &R)
Definition: PatternMatch.h:419
Value * SimplifyVectorOp(BinaryOperator &Inst)
Makes transformation of binary operation specific for vector types.
Value * CreateSRem(Value *LHS, Value *RHS, const Twine &Name="")
Definition: IRBuilder.h:736
class_match< Constant > m_Constant()
Definition: PatternMatch.h:78
static RegisterPass< NVPTXAllocaHoisting > X("alloca-hoisting","Hoisting alloca instructions in non-entry ""blocks to the entry block")
Instruction * FoldOpIntoSelect(Instruction &Op, SelectInst *SI)
Value * CreateSub(Value *LHS, Value *RHS, const Twine &Name="", bool HasNUW=false, bool HasNSW=false)
Definition: IRBuilder.h:654
static Constant * getNullValue(Type *Ty)
Definition: Constants.cpp:133
StringRef getName() const
Definition: Value.cpp:168
bool isNegative() const
Determine sign of this APInt.
Definition: APInt.h:320
bool match(Val *V, const Pattern &P)
Definition: PatternMatch.h:42
void setHasNoUnsignedWrap(bool b=true)
BinaryOp_match< LHS, RHS, Instruction::Add > m_Add(const LHS &L, const RHS &R)
Definition: PatternMatch.h:395
Value * SimplifyUsingDistributiveLaws(BinaryOperator &I)
class_match< ConstantInt > m_ConstantInt()
m_ConstantInt() - Match an arbitrary ConstantInt and ignore it.
Definition: PatternMatch.h:72
void takeName(Value *V)
Definition: Value.cpp:242
Value * dyn_castNegVal(Value *V) const
OneUse_match< T > m_OneUse(const T &SubPattern)
Definition: PatternMatch.h:60
apint_match m_APInt(const APInt *&Res)
Definition: PatternMatch.h:183
bool isVectorTy() const
Definition: Type.h:226
LLVM Constant Representation.
Definition: Constant.h:41
Instruction * ReplaceInstUsesWith(Instruction &I, Value *V)
Definition: InstCombine.h:282
cst_pred_ty< is_all_ones > m_AllOnes()
m_AllOnes() - Match an integer or vector with all bits set to true.
Definition: PatternMatch.h:265
BinaryOp_match< LHS, RHS, Instruction::Shl > m_Shl(const LHS &L, const RHS &R)
Definition: PatternMatch.h:485
Value * getOperand(unsigned i) const
Definition: User.h:90
BuilderTy * Builder
Definition: InstCombine.h:102
NUW NUW NUW NUW Exact static Exact BinaryOperator * CreateNeg(Value *Op, const Twine &Name="", Instruction *InsertBefore=nullptr)
bool hasNoSignedWrap() const
hasNoSignedWrap - Determine whether the no signed wrap flag is set.
bool isPowerOf2() const
Check if this APInt's value is a power of two greater than zero.
Definition: APInt.h:388
BinaryOps getOpcode() const
Definition: InstrTypes.h:326
bool SimplifyAssociativeOrCommutative(BinaryOperator &I)
Class for constant integers.
Definition: Constants.h:51
unsigned logBase2() const
Definition: APInt.h:1496
Type * getType() const
Definition: Value.h:215
Value * CreateMul(Value *LHS, Value *RHS, const Twine &Name="", bool HasNUW=false, bool HasNSW=false)
Definition: IRBuilder.h:676
static Constant * get(Type *Ty, uint64_t V, bool isSigned=false)
Definition: Constants.cpp:528
Value * CreateURem(Value *LHS, Value *RHS, const Twine &Name="")
Definition: IRBuilder.h:730
Class for arbitrary precision integers.
Definition: APInt.h:75
bool isIntegerTy() const
Definition: Type.h:193
LLVM_ATTRIBUTE_UNUSED_RESULT std::enable_if< !is_simple_type< Y >::value, typename cast_retty< X, const Y >::ret_type >::type dyn_cast(const Y &Val)
Definition: Casting.h:265
const Type * getScalarType() const LLVM_READONLY
Definition: Type.cpp:51
bool hasOneUse() const
Definition: Value.h:284
static Constant * getShl(Constant *C1, Constant *C2, bool HasNUW=false, bool HasNSW=false)
Definition: Constants.cpp:2173
bool MaskedValueIsZero(Value *V, const APInt &Mask, unsigned Depth=0) const
Definition: InstCombine.h:323
const DataLayout * DL
Definition: InstCombine.h:89
Value * CreateNeg(Value *V, const Twine &Name="", bool HasNUW=false, bool HasNSW=false)
Definition: IRBuilder.h:863
unsigned getPrimitiveSizeInBits() const LLVM_READONLY
Definition: Type.cpp:117
LLVM Value Representation.
Definition: Value.h:69
bool hasNoUnsignedWrap() const
hasNoUnsignedWrap - Determine whether the no unsigned wrap flag is set.
cst_pred_ty< is_one > m_One()
m_One() - Match an integer 1 or a vector with all elements equal to 1.
Definition: PatternMatch.h:257
static GCMetadataPrinterRegistry::Add< OcamlGCMetadataPrinter > Y("ocaml","ocaml 3.10-compatible collector")
Value * SimplifyMulInst(Value *LHS, Value *RHS, const DataLayout *TD=nullptr, const TargetLibraryInfo *TLI=nullptr, const DominatorTree *DT=nullptr)
Instruction * InstCombiner::visitOr ( BinaryOperator I)

Definition at line 1909 of file InstCombineAndOrXor.cpp.

1909  {
1910  bool Changed = SimplifyAssociativeOrCommutative(I);
1911  Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
1912 
1913  if (Value *V = SimplifyVectorOp(I))
1914  return ReplaceInstUsesWith(I, V);
1915 
1916  if (Value *V = SimplifyOrInst(Op0, Op1, DL))
1917  return ReplaceInstUsesWith(I, V);
1918 
1919  // (A&B)|(A&C) -> A&(B|C) etc
1921  return ReplaceInstUsesWith(I, V);
1922 
1923  // See if we can simplify any instructions used by the instruction whose sole
1924  // purpose is to compute bits we don't care about.
1926  return &I;
1927 
1928  if (ConstantInt *RHS = dyn_cast<ConstantInt>(Op1)) {
1929  ConstantInt *C1 = nullptr; Value *X = nullptr;
1930  // (X & C1) | C2 --> (X | C2) & (C1|C2)
1931  // iff (C1 & C2) == 0.
1932  if (match(Op0, m_And(m_Value(X), m_ConstantInt(C1))) &&
1933  (RHS->getValue() & C1->getValue()) != 0 &&
1934  Op0->hasOneUse()) {
1935  Value *Or = Builder->CreateOr(X, RHS);
1936  Or->takeName(Op0);
1937  return BinaryOperator::CreateAnd(Or,
1938  Builder->getInt(RHS->getValue() | C1->getValue()));
1939  }
1940 
1941  // (X ^ C1) | C2 --> (X | C2) ^ (C1&~C2)
1942  if (match(Op0, m_Xor(m_Value(X), m_ConstantInt(C1))) &&
1943  Op0->hasOneUse()) {
1944  Value *Or = Builder->CreateOr(X, RHS);
1945  Or->takeName(Op0);
1946  return BinaryOperator::CreateXor(Or,
1947  Builder->getInt(C1->getValue() & ~RHS->getValue()));
1948  }
1949 
1950  // Try to fold constant and into select arguments.
1951  if (SelectInst *SI = dyn_cast<SelectInst>(Op0))
1952  if (Instruction *R = FoldOpIntoSelect(I, SI))
1953  return R;
1954 
1955  if (isa<PHINode>(Op0))
1956  if (Instruction *NV = FoldOpIntoPhi(I))
1957  return NV;
1958  }
1959 
1960  Value *A = nullptr, *B = nullptr;
1961  ConstantInt *C1 = nullptr, *C2 = nullptr;
1962 
1963  // (A | B) | C and A | (B | C) -> bswap if possible.
1964  // (A >> B) | (C << D) and (A << B) | (B >> C) -> bswap if possible.
1965  if (match(Op0, m_Or(m_Value(), m_Value())) ||
1966  match(Op1, m_Or(m_Value(), m_Value())) ||
1967  (match(Op0, m_LogicalShift(m_Value(), m_Value())) &&
1968  match(Op1, m_LogicalShift(m_Value(), m_Value())))) {
1969  if (Instruction *BSwap = MatchBSwap(I))
1970  return BSwap;
1971  }
1972 
1973  // (X^C)|Y -> (X|Y)^C iff Y&C == 0
1974  if (Op0->hasOneUse() &&
1975  match(Op0, m_Xor(m_Value(A), m_ConstantInt(C1))) &&
1976  MaskedValueIsZero(Op1, C1->getValue())) {
1977  Value *NOr = Builder->CreateOr(A, Op1);
1978  NOr->takeName(Op0);
1979  return BinaryOperator::CreateXor(NOr, C1);
1980  }
1981 
1982  // Y|(X^C) -> (X|Y)^C iff Y&C == 0
1983  if (Op1->hasOneUse() &&
1984  match(Op1, m_Xor(m_Value(A), m_ConstantInt(C1))) &&
1985  MaskedValueIsZero(Op0, C1->getValue())) {
1986  Value *NOr = Builder->CreateOr(A, Op0);
1987  NOr->takeName(Op0);
1988  return BinaryOperator::CreateXor(NOr, C1);
1989  }
1990 
1991  // (A & C)|(B & D)
1992  Value *C = nullptr, *D = nullptr;
1993  if (match(Op0, m_And(m_Value(A), m_Value(C))) &&
1994  match(Op1, m_And(m_Value(B), m_Value(D)))) {
1995  Value *V1 = nullptr, *V2 = nullptr;
1996  C1 = dyn_cast<ConstantInt>(C);
1997  C2 = dyn_cast<ConstantInt>(D);
1998  if (C1 && C2) { // (A & C1)|(B & C2)
1999  if ((C1->getValue() & C2->getValue()) == 0) {
2000  // ((V | N) & C1) | (V & C2) --> (V|N) & (C1|C2)
2001  // iff (C1&C2) == 0 and (N&~C1) == 0
2002  if (match(A, m_Or(m_Value(V1), m_Value(V2))) &&
2003  ((V1 == B && MaskedValueIsZero(V2, ~C1->getValue())) || // (V|N)
2004  (V2 == B && MaskedValueIsZero(V1, ~C1->getValue())))) // (N|V)
2005  return BinaryOperator::CreateAnd(A,
2006  Builder->getInt(C1->getValue()|C2->getValue()));
2007  // Or commutes, try both ways.
2008  if (match(B, m_Or(m_Value(V1), m_Value(V2))) &&
2009  ((V1 == A && MaskedValueIsZero(V2, ~C2->getValue())) || // (V|N)
2010  (V2 == A && MaskedValueIsZero(V1, ~C2->getValue())))) // (N|V)
2011  return BinaryOperator::CreateAnd(B,
2012  Builder->getInt(C1->getValue()|C2->getValue()));
2013 
2014  // ((V|C3)&C1) | ((V|C4)&C2) --> (V|C3|C4)&(C1|C2)
2015  // iff (C1&C2) == 0 and (C3&~C1) == 0 and (C4&~C2) == 0.
2016  ConstantInt *C3 = nullptr, *C4 = nullptr;
2017  if (match(A, m_Or(m_Value(V1), m_ConstantInt(C3))) &&
2018  (C3->getValue() & ~C1->getValue()) == 0 &&
2019  match(B, m_Or(m_Specific(V1), m_ConstantInt(C4))) &&
2020  (C4->getValue() & ~C2->getValue()) == 0) {
2021  V2 = Builder->CreateOr(V1, ConstantExpr::getOr(C3, C4), "bitfield");
2022  return BinaryOperator::CreateAnd(V2,
2023  Builder->getInt(C1->getValue()|C2->getValue()));
2024  }
2025  }
2026  }
2027 
2028  // (A & (C0?-1:0)) | (B & ~(C0?-1:0)) -> C0 ? A : B, and commuted variants.
2029  // Don't do this for vector select idioms, the code generator doesn't handle
2030  // them well yet.
2031  if (!I.getType()->isVectorTy()) {
2032  if (Instruction *Match = MatchSelectFromAndOr(A, B, C, D))
2033  return Match;
2034  if (Instruction *Match = MatchSelectFromAndOr(B, A, D, C))
2035  return Match;
2036  if (Instruction *Match = MatchSelectFromAndOr(C, B, A, D))
2037  return Match;
2038  if (Instruction *Match = MatchSelectFromAndOr(D, A, B, C))
2039  return Match;
2040  }
2041 
2042  // ((A&~B)|(~A&B)) -> A^B
2043  if ((match(C, m_Not(m_Specific(D))) &&
2044  match(B, m_Not(m_Specific(A)))))
2045  return BinaryOperator::CreateXor(A, D);
2046  // ((~B&A)|(~A&B)) -> A^B
2047  if ((match(A, m_Not(m_Specific(D))) &&
2048  match(B, m_Not(m_Specific(C)))))
2049  return BinaryOperator::CreateXor(C, D);
2050  // ((A&~B)|(B&~A)) -> A^B
2051  if ((match(C, m_Not(m_Specific(B))) &&
2052  match(D, m_Not(m_Specific(A)))))
2053  return BinaryOperator::CreateXor(A, B);
2054  // ((~B&A)|(B&~A)) -> A^B
2055  if ((match(A, m_Not(m_Specific(B))) &&
2056  match(D, m_Not(m_Specific(C)))))
2057  return BinaryOperator::CreateXor(C, B);
2058 
2059  // ((A|B)&1)|(B&-2) -> (A&1) | B
2060  if (match(A, m_Or(m_Value(V1), m_Specific(B))) ||
2061  match(A, m_Or(m_Specific(B), m_Value(V1)))) {
2062  Instruction *Ret = FoldOrWithConstants(I, Op1, V1, B, C);
2063  if (Ret) return Ret;
2064  }
2065  // (B&-2)|((A|B)&1) -> (A&1) | B
2066  if (match(B, m_Or(m_Specific(A), m_Value(V1))) ||
2067  match(B, m_Or(m_Value(V1), m_Specific(A)))) {
2068  Instruction *Ret = FoldOrWithConstants(I, Op0, A, V1, D);
2069  if (Ret) return Ret;
2070  }
2071  }
2072 
2073  // (X >> Z) | (Y >> Z) -> (X|Y) >> Z for all shifts.
2074  if (BinaryOperator *SI1 = dyn_cast<BinaryOperator>(Op1)) {
2075  if (BinaryOperator *SI0 = dyn_cast<BinaryOperator>(Op0))
2076  if (SI0->isShift() && SI0->getOpcode() == SI1->getOpcode() &&
2077  SI0->getOperand(1) == SI1->getOperand(1) &&
2078  (SI0->hasOneUse() || SI1->hasOneUse())) {
2079  Value *NewOp = Builder->CreateOr(SI0->getOperand(0), SI1->getOperand(0),
2080  SI0->getName());
2081  return BinaryOperator::Create(SI1->getOpcode(), NewOp,
2082  SI1->getOperand(1));
2083  }
2084  }
2085 
2086  // (~A | ~B) == (~(A & B)) - De Morgan's Law
2087  if (Value *Op0NotVal = dyn_castNotVal(Op0))
2088  if (Value *Op1NotVal = dyn_castNotVal(Op1))
2089  if (Op0->hasOneUse() && Op1->hasOneUse()) {
2090  Value *And = Builder->CreateAnd(Op0NotVal, Op1NotVal,
2091  I.getName()+".demorgan");
2092  return BinaryOperator::CreateNot(And);
2093  }
2094 
2095  // Canonicalize xor to the RHS.
2096  bool SwappedForXor = false;
2097  if (match(Op0, m_Xor(m_Value(), m_Value()))) {
2098  std::swap(Op0, Op1);
2099  SwappedForXor = true;
2100  }
2101 
2102  // A | ( A ^ B) -> A | B
2103  // A | (~A ^ B) -> A | ~B
2104  // (A & B) | (A ^ B)
2105  if (match(Op1, m_Xor(m_Value(A), m_Value(B)))) {
2106  if (Op0 == A || Op0 == B)
2107  return BinaryOperator::CreateOr(A, B);
2108 
2109  if (match(Op0, m_And(m_Specific(A), m_Specific(B))) ||
2110  match(Op0, m_And(m_Specific(B), m_Specific(A))))
2111  return BinaryOperator::CreateOr(A, B);
2112 
2113  if (Op1->hasOneUse() && match(A, m_Not(m_Specific(Op0)))) {
2114  Value *Not = Builder->CreateNot(B, B->getName()+".not");
2115  return BinaryOperator::CreateOr(Not, Op0);
2116  }
2117  if (Op1->hasOneUse() && match(B, m_Not(m_Specific(Op0)))) {
2118  Value *Not = Builder->CreateNot(A, A->getName()+".not");
2119  return BinaryOperator::CreateOr(Not, Op0);
2120  }
2121  }
2122 
2123  // A | ~(A | B) -> A | ~B
2124  // A | ~(A ^ B) -> A | ~B
2125  if (match(Op1, m_Not(m_Value(A))))
2126  if (BinaryOperator *B = dyn_cast<BinaryOperator>(A))
2127  if ((Op0 == B->getOperand(0) || Op0 == B->getOperand(1)) &&
2128  Op1->hasOneUse() && (B->getOpcode() == Instruction::Or ||
2129  B->getOpcode() == Instruction::Xor)) {
2130  Value *NotOp = Op0 == B->getOperand(0) ? B->getOperand(1) :
2131  B->getOperand(0);
2132  Value *Not = Builder->CreateNot(NotOp, NotOp->getName()+".not");
2133  return BinaryOperator::CreateOr(Not, Op0);
2134  }
2135 
2136  if (SwappedForXor)
2137  std::swap(Op0, Op1);
2138 
2139  if (ICmpInst *RHS = dyn_cast<ICmpInst>(I.getOperand(1)))
2140  if (ICmpInst *LHS = dyn_cast<ICmpInst>(I.getOperand(0)))
2141  if (Value *Res = FoldOrOfICmps(LHS, RHS))
2142  return ReplaceInstUsesWith(I, Res);
2143 
2144  // (fcmp uno x, c) | (fcmp uno y, c) -> (fcmp uno x, y)
2145  if (FCmpInst *LHS = dyn_cast<FCmpInst>(I.getOperand(0)))
2146  if (FCmpInst *RHS = dyn_cast<FCmpInst>(I.getOperand(1)))
2147  if (Value *Res = FoldOrOfFCmps(LHS, RHS))
2148  return ReplaceInstUsesWith(I, Res);
2149 
2150  // fold (or (cast A), (cast B)) -> (cast (or A, B))
2151  if (CastInst *Op0C = dyn_cast<CastInst>(Op0)) {
2152  CastInst *Op1C = dyn_cast<CastInst>(Op1);
2153  if (Op1C && Op0C->getOpcode() == Op1C->getOpcode()) {// same cast kind ?
2154  Type *SrcTy = Op0C->getOperand(0)->getType();
2155  if (SrcTy == Op1C->getOperand(0)->getType() &&
2156  SrcTy->isIntOrIntVectorTy()) {
2157  Value *Op0COp = Op0C->getOperand(0), *Op1COp = Op1C->getOperand(0);
2158 
2159  if ((!isa<ICmpInst>(Op0COp) || !isa<ICmpInst>(Op1COp)) &&
2160  // Only do this if the casts both really cause code to be
2161  // generated.
2162  ShouldOptimizeCast(Op0C->getOpcode(), Op0COp, I.getType()) &&
2163  ShouldOptimizeCast(Op1C->getOpcode(), Op1COp, I.getType())) {
2164  Value *NewOp = Builder->CreateOr(Op0COp, Op1COp, I.getName());
2165  return CastInst::Create(Op0C->getOpcode(), NewOp, I.getType());
2166  }
2167 
2168  // If this is or(cast(icmp), cast(icmp)), try to fold this even if the
2169  // cast is otherwise not optimizable. This happens for vector sexts.
2170  if (ICmpInst *RHS = dyn_cast<ICmpInst>(Op1COp))
2171  if (ICmpInst *LHS = dyn_cast<ICmpInst>(Op0COp))
2172  if (Value *Res = FoldOrOfICmps(LHS, RHS))
2173  return CastInst::Create(Op0C->getOpcode(), Res, I.getType());
2174 
2175  // If this is or(cast(fcmp), cast(fcmp)), try to fold this even if the
2176  // cast is otherwise not optimizable. This happens for vector sexts.
2177  if (FCmpInst *RHS = dyn_cast<FCmpInst>(Op1COp))
2178  if (FCmpInst *LHS = dyn_cast<FCmpInst>(Op0COp))
2179  if (Value *Res = FoldOrOfFCmps(LHS, RHS))
2180  return CastInst::Create(Op0C->getOpcode(), Res, I.getType());
2181  }
2182  }
2183  }
2184 
2185  // or(sext(A), B) -> A ? -1 : B where A is an i1
2186  // or(A, sext(B)) -> B ? -1 : A where B is an i1
2187  if (match(Op0, m_SExt(m_Value(A))) && A->getType()->isIntegerTy(1))
2188  return SelectInst::Create(A, ConstantInt::getSigned(I.getType(), -1), Op1);
2189  if (match(Op1, m_SExt(m_Value(A))) && A->getType()->isIntegerTy(1))
2190  return SelectInst::Create(A, ConstantInt::getSigned(I.getType(), -1), Op0);
2191 
2192  // Note: If we've gotten to the point of visiting the outer OR, then the
2193  // inner one couldn't be simplified. If it was a constant, then it won't
2194  // be simplified by a later pass either, so we try swapping the inner/outer
2195  // ORs in the hopes that we'll be able to simplify it this way.
2196  // (X|C) | V --> (X|V) | C
2197  if (Op0->hasOneUse() && !isa<ConstantInt>(Op1) &&
2198  match(Op0, m_Or(m_Value(A), m_ConstantInt(C1)))) {
2199  Value *Inner = Builder->CreateOr(A, Op1);
2200  Inner->takeName(Op0);
2201  return BinaryOperator::CreateOr(Inner, C1);
2202  }
2203 
2204  // Change (or (bool?A:B),(bool?C:D)) --> (bool?(or A,C):(or B,D))
2205  // Since this OR statement hasn't been optimized further yet, we hope
2206  // that this transformation will allow the new ORs to be optimized.
2207  {
2208  Value *X = nullptr, *Y = nullptr;
2209  if (Op0->hasOneUse() && Op1->hasOneUse() &&
2210  match(Op0, m_Select(m_Value(X), m_Value(A), m_Value(B))) &&
2211  match(Op1, m_Select(m_Value(Y), m_Value(C), m_Value(D))) && X == Y) {
2212  Value *orTrue = Builder->CreateOr(A, C);
2213  Value *orFalse = Builder->CreateOr(B, D);
2214  return SelectInst::Create(X, orTrue, orFalse);
2215  }
2216  }
2217 
2218  return Changed ? &I : nullptr;
2219 }
Instruction * FoldOpIntoPhi(Instruction &I)
BinaryOp_match< LHS, RHS, Instruction::And > m_And(const LHS &L, const RHS &R)
Definition: PatternMatch.h:467
Instruction::CastOps getOpcode() const
Return the opcode of this CastInst.
Definition: InstrTypes.h:624
class_match< Value > m_Value()
m_Value() - Match an arbitrary value and ignore it.
Definition: PatternMatch.h:70
static BinaryOperator * CreateNot(Value *Op, const Twine &Name="", Instruction *InsertBefore=nullptr)
bool ShouldOptimizeCast(Instruction::CastOps opcode, const Value *V, Type *Ty)
Value * SimplifyVectorOp(BinaryOperator &Inst)
Makes transformation of binary operation specific for vector types.
static RegisterPass< NVPTXAllocaHoisting > X("alloca-hoisting","Hoisting alloca instructions in non-entry ""blocks to the entry block")
Instruction * FoldOpIntoSelect(Instruction &Op, SelectInst *SI)
StringRef getName() const
Definition: Value.cpp:168
APInt Not(const APInt &APIVal)
Bitwise complement function.
Definition: APInt.h:1880
bool match(Val *V, const Pattern &P)
Definition: PatternMatch.h:42
BinaryOp_match< LHS, RHS, Instruction::Xor > m_Xor(const LHS &L, const RHS &R)
Definition: PatternMatch.h:479
Base class of casting instructions.
Definition: InstrTypes.h:387
const APInt & getValue() const
Return the constant's value.
Definition: Constants.h:105
Instruction * FoldOrWithConstants(BinaryOperator &I, Value *Op, Value *A, Value *B, Value *C)
static Instruction * MatchSelectFromAndOr(Value *A, Value *B, Value *C, Value *D)
not_match< LHS > m_Not(const LHS &L)
Definition: PatternMatch.h:828
Value * CreateAnd(Value *LHS, Value *RHS, const Twine &Name="")
Definition: IRBuilder.h:806
Value * SimplifyOrInst(Value *LHS, Value *RHS, const DataLayout *TD=nullptr, const TargetLibraryInfo *TLI=nullptr, const DominatorTree *DT=nullptr)
static SelectInst * Create(Value *C, Value *S1, Value *S2, const Twine &NameStr="", Instruction *InsertBefore=nullptr)
Value * CreateOr(Value *LHS, Value *RHS, const Twine &Name="")
Definition: IRBuilder.h:822
Value * SimplifyUsingDistributiveLaws(BinaryOperator &I)
Represents a floating point comparison operator.
class_match< ConstantInt > m_ConstantInt()
m_ConstantInt() - Match an arbitrary ConstantInt and ignore it.
Definition: PatternMatch.h:72
SelectClass_match< Cond, LHS, RHS > m_Select(const Cond &C, const LHS &L, const RHS &R)
Definition: PatternMatch.h:720
void takeName(Value *V)
Definition: Value.cpp:242
Value * FoldOrOfICmps(ICmpInst *LHS, ICmpInst *RHS)
FoldOrOfICmps - Fold (icmp)|(icmp) if possible.
bool isIntOrIntVectorTy() const
Definition: Type.h:201
BinaryOp_match< LHS, RHS, Instruction::Or > m_Or(const LHS &L, const RHS &R)
Definition: PatternMatch.h:473
bool isVectorTy() const
Definition: Type.h:226
Value * CreateNot(Value *V, const Twine &Name="")
Definition: IRBuilder.h:885
Instruction * ReplaceInstUsesWith(Instruction &I, Value *V)
Definition: InstCombine.h:282
APInt Or(const APInt &LHS, const APInt &RHS)
Bitwise OR function for APInt.
Definition: APInt.h:1870
APInt Xor(const APInt &LHS, const APInt &RHS)
Bitwise XOR function for APInt.
Definition: APInt.h:1875
specificval_ty m_Specific(const Value *V)
m_Specific - Match if we have a specific specified value.
Definition: PatternMatch.h:323
Represent an integer comparison operator.
Definition: Instructions.h:977
Value * getOperand(unsigned i) const
Definition: User.h:90
BuilderTy * Builder
Definition: InstCombine.h:102
CastClass_match< OpTy, Instruction::SExt > m_SExt(const OpTy &Op)
m_SExt
Definition: PatternMatch.h:775
Instruction * MatchBSwap(BinaryOperator &I)
Definition: test.h:1
bool SimplifyAssociativeOrCommutative(BinaryOperator &I)
Class for constant integers.
Definition: Constants.h:51
Type * getType() const
Definition: Value.h:215
BinOp2_match< LHS, RHS, Instruction::LShr, Instruction::Shl > m_LogicalShift(const LHS &L, const RHS &R)
m_LogicalShift - Matches LShr or Shl.
Definition: PatternMatch.h:625
static ConstantInt * getSigned(IntegerType *Ty, int64_t V)
Get a ConstantInt for a specific signed value.
Definition: Constants.cpp:543
void swap(llvm::BitVector &LHS, llvm::BitVector &RHS)
Implement std::swap in terms of BitVector swap.
Definition: BitVector.h:590
bool isIntegerTy() const
Definition: Type.h:193
static BinaryOperator * Create(BinaryOps Op, Value *S1, Value *S2, const Twine &Name=Twine(), Instruction *InsertBefore=nullptr)
LLVM_ATTRIBUTE_UNUSED_RESULT std::enable_if< !is_simple_type< Y >::value, typename cast_retty< X, const Y >::ret_type >::type dyn_cast(const Y &Val)
Definition: Casting.h:265
APInt And(const APInt &LHS, const APInt &RHS)
Bitwise AND function for APInt.
Definition: APInt.h:1865
static CastInst * Create(Instruction::CastOps, Value *S, Type *Ty, const Twine &Name="", Instruction *InsertBefore=nullptr)
Construct any of the CastInst subclasses.
static Value * dyn_castNotVal(Value *V)
#define I(x, y, z)
Definition: MD5.cpp:54
static Constant * getOr(Constant *C1, Constant *C2)
Definition: Constants.cpp:2165
bool hasOneUse() const
Definition: Value.h:284
bool MaskedValueIsZero(Value *V, const APInt &Mask, unsigned Depth=0) const
Definition: InstCombine.h:323
const DataLayout * DL
Definition: InstCombine.h:89
LLVM Value Representation.
Definition: Value.h:69
static GCMetadataPrinterRegistry::Add< OcamlGCMetadataPrinter > Y("ocaml","ocaml 3.10-compatible collector")
bool SimplifyDemandedInstructionBits(Instruction &Inst)
ConstantInt * getInt(const APInt &AI)
Get a constant integer value.
Definition: IRBuilder.h:292
Value * FoldOrOfFCmps(FCmpInst *LHS, FCmpInst *RHS)
Instruction * InstCombiner::visitPHINode ( PHINode PN)

Definition at line 790 of file InstCombinePHI.cpp.

790  {
791  if (Value *V = SimplifyInstruction(&PN, DL, TLI))
792  return ReplaceInstUsesWith(PN, V);
793 
794  // If all PHI operands are the same operation, pull them through the PHI,
795  // reducing code size.
796  if (isa<Instruction>(PN.getIncomingValue(0)) &&
797  isa<Instruction>(PN.getIncomingValue(1)) &&
798  cast<Instruction>(PN.getIncomingValue(0))->getOpcode() ==
799  cast<Instruction>(PN.getIncomingValue(1))->getOpcode() &&
800  // FIXME: The hasOneUse check will fail for PHIs that use the value more
801  // than themselves more than once.
802  PN.getIncomingValue(0)->hasOneUse())
803  if (Instruction *Result = FoldPHIArgOpIntoPHI(PN))
804  return Result;
805 
806  // If this is a trivial cycle in the PHI node graph, remove it. Basically, if
807  // this PHI only has a single use (a PHI), and if that PHI only has one use (a
808  // PHI)... break the cycle.
809  if (PN.hasOneUse()) {
810  Instruction *PHIUser = cast<Instruction>(PN.user_back());
811  if (PHINode *PU = dyn_cast<PHINode>(PHIUser)) {
812  SmallPtrSet<PHINode*, 16> PotentiallyDeadPHIs;
813  PotentiallyDeadPHIs.insert(&PN);
814  if (DeadPHICycle(PU, PotentiallyDeadPHIs))
815  return ReplaceInstUsesWith(PN, UndefValue::get(PN.getType()));
816  }
817 
818  // If this phi has a single use, and if that use just computes a value for
819  // the next iteration of a loop, delete the phi. This occurs with unused
820  // induction variables, e.g. "for (int j = 0; ; ++j);". Detecting this
821  // common case here is good because the only other things that catch this
822  // are induction variable analysis (sometimes) and ADCE, which is only run
823  // late.
824  if (PHIUser->hasOneUse() &&
825  (isa<BinaryOperator>(PHIUser) || isa<GetElementPtrInst>(PHIUser)) &&
826  PHIUser->user_back() == &PN) {
827  return ReplaceInstUsesWith(PN, UndefValue::get(PN.getType()));
828  }
829  }
830 
831  // We sometimes end up with phi cycles that non-obviously end up being the
832  // same value, for example:
833  // z = some value; x = phi (y, z); y = phi (x, z)
834  // where the phi nodes don't necessarily need to be in the same block. Do a
835  // quick check to see if the PHI node only contains a single non-phi value, if
836  // so, scan to see if the phi cycle is actually equal to that value.
837  {
838  unsigned InValNo = 0, NumIncomingVals = PN.getNumIncomingValues();
839  // Scan for the first non-phi operand.
840  while (InValNo != NumIncomingVals &&
841  isa<PHINode>(PN.getIncomingValue(InValNo)))
842  ++InValNo;
843 
844  if (InValNo != NumIncomingVals) {
845  Value *NonPhiInVal = PN.getIncomingValue(InValNo);
846 
847  // Scan the rest of the operands to see if there are any conflicts, if so
848  // there is no need to recursively scan other phis.
849  for (++InValNo; InValNo != NumIncomingVals; ++InValNo) {
850  Value *OpVal = PN.getIncomingValue(InValNo);
851  if (OpVal != NonPhiInVal && !isa<PHINode>(OpVal))
852  break;
853  }
854 
855  // If we scanned over all operands, then we have one unique value plus
856  // phi values. Scan PHI nodes to see if they all merge in each other or
857  // the value.
858  if (InValNo == NumIncomingVals) {
859  SmallPtrSet<PHINode*, 16> ValueEqualPHIs;
860  if (PHIsEqualValue(&PN, NonPhiInVal, ValueEqualPHIs))
861  return ReplaceInstUsesWith(PN, NonPhiInVal);
862  }
863  }
864  }
865 
866  // If there are multiple PHIs, sort their operands so that they all list
867  // the blocks in the same order. This will help identical PHIs be eliminated
868  // by other passes. Other passes shouldn't depend on this for correctness
869  // however.
870  PHINode *FirstPN = cast<PHINode>(PN.getParent()->begin());
871  if (&PN != FirstPN)
872  for (unsigned i = 0, e = FirstPN->getNumIncomingValues(); i != e; ++i) {
873  BasicBlock *BBA = PN.getIncomingBlock(i);
874  BasicBlock *BBB = FirstPN->getIncomingBlock(i);
875  if (BBA != BBB) {
876  Value *VA = PN.getIncomingValue(i);
877  unsigned j = PN.getBasicBlockIndex(BBB);
878  Value *VB = PN.getIncomingValue(j);
879  PN.setIncomingBlock(i, BBB);
880  PN.setIncomingValue(i, VB);
881  PN.setIncomingBlock(j, BBA);
882  PN.setIncomingValue(j, VA);
883  // NOTE: Instcombine normally would want us to "return &PN" if we
884  // modified any of the operands of an instruction. However, since we
885  // aren't adding or removing uses (just rearranging them) we don't do
886  // this in this case.
887  }
888  }
889 
890  // If this is an integer PHI and we know that it has an illegal type, see if
891  // it is only used by trunc or trunc(lshr) operations. If so, we split the
892  // PHI into the various pieces being extracted. This sort of thing is
893  // introduced when SROA promotes an aggregate to a single large integer type.
894  if (PN.getType()->isIntegerTy() && DL &&
896  if (Instruction *Res = SliceUpIllegalIntegerPHI(PN))
897  return Res;
898 
899  return nullptr;
900 }
bool insert(PtrType Ptr)
Definition: SmallPtrSet.h:256
static bool PHIsEqualValue(PHINode *PN, Value *NonPhiInVal, SmallPtrSet< PHINode *, 16 > &ValueEqualPHIs)
iterator begin()
Definition: BasicBlock.h:198
TargetLibraryInfo * TLI
Definition: InstCombine.h:90
Instruction * SliceUpIllegalIntegerPHI(PHINode &PN)
unsigned getNumIncomingValues() const
LLVM Basic Block Representation.
Definition: BasicBlock.h:72
Instruction * ReplaceInstUsesWith(Instruction &I, Value *V)
Definition: InstCombine.h:282
BasicBlock * getIncomingBlock(unsigned i) const
static UndefValue * get(Type *T)
Definition: Constants.cpp:1372
Value * SimplifyInstruction(Instruction *I, const DataLayout *TD=nullptr, const TargetLibraryInfo *TLI=nullptr, const DominatorTree *DT=nullptr)
static bool DeadPHICycle(PHINode *PN, SmallPtrSet< PHINode *, 16 > &PotentiallyDeadPHIs)
void setIncomingBlock(unsigned i, BasicBlock *BB)
Value * getIncomingValue(unsigned i) const
Type * getType() const
Definition: Value.h:215
Instruction * user_back()
Definition: Instruction.h:50
bool isIntegerTy() const
Definition: Type.h:193
bool isLegalInteger(unsigned Width) const
Definition: DataLayout.h:224
Instruction * FoldPHIArgOpIntoPHI(PHINode &PN)
bool hasOneUse() const
Definition: Value.h:284
const DataLayout * DL
Definition: InstCombine.h:89
unsigned getPrimitiveSizeInBits() const LLVM_READONLY
Definition: Type.cpp:117
LLVM Value Representation.
Definition: Value.h:69
void setIncomingValue(unsigned i, Value *V)
int getBasicBlockIndex(const BasicBlock *BB) const
const BasicBlock * getParent() const
Definition: Instruction.h:53
Instruction * InstCombiner::visitPtrToInt ( PtrToIntInst CI)

Definition at line 1490 of file InstCombineCasts.cpp.

1490  {
1491  // If the destination integer type is not the intptr_t type for this target,
1492  // do a ptrtoint to intptr_t then do a trunc or zext. This allows the cast
1493  // to be exposed to other transforms.
1494 
1495  if (!DL)
1496  return commonPointerCastTransforms(CI);
1497 
1498  Type *Ty = CI.getType();
1499  unsigned AS = CI.getPointerAddressSpace();
1500 
1501  if (Ty->getScalarSizeInBits() == DL->getPointerSizeInBits(AS))
1502  return commonPointerCastTransforms(CI);
1503 
1504  Type *PtrTy = DL->getIntPtrType(CI.getContext(), AS);
1505  if (Ty->isVectorTy()) // Handle vectors of pointers.
1506  PtrTy = VectorType::get(PtrTy, Ty->getVectorNumElements());
1507 
1508  Value *P = Builder->CreatePtrToInt(CI.getOperand(0), PtrTy);
1509  return CastInst::CreateIntegerCast(P, Ty, /*isSigned=*/false);
1510 }
#define P(N)
bool isVectorTy() const
Definition: Type.h:226
Value * CreatePtrToInt(Value *V, Type *DestTy, const Twine &Name="")
Definition: IRBuilder.h:1150
unsigned getPointerAddressSpace() const
Returns the address space of the pointer operand.
Value * getOperand(unsigned i) const
Definition: User.h:90
BuilderTy * Builder
Definition: InstCombine.h:102
LLVMContext & getContext() const
All values hold a context through their type.
Definition: Value.cpp:615
IntegerType * getIntPtrType(LLVMContext &C, unsigned AddressSpace=0) const
Definition: DataLayout.cpp:703
static CastInst * CreateIntegerCast(Value *S, Type *Ty, bool isSigned, const Twine &Name="", Instruction *InsertBefore=nullptr)
Create a ZExt, BitCast, or Trunc for int -> int casts.
unsigned getVectorNumElements() const
Definition: Type.cpp:208
unsigned getScalarSizeInBits() const LLVM_READONLY
Definition: Type.cpp:135
Type * getType() const
Definition: Value.h:215
unsigned getPointerSizeInBits(unsigned AS=0) const
Definition: DataLayout.h:313
const DataLayout * DL
Definition: InstCombine.h:89
LLVM Value Representation.
Definition: Value.h:69
static VectorType * get(Type *ElementType, unsigned NumElements)
Definition: Type.cpp:703
Instruction * commonPointerCastTransforms(CastInst &CI)
Implement the transforms for cast of pointer (bitcast/ptrtoint)
Instruction * llvm::InstVisitor< InstCombiner , Instruction * >::visitPtrToIntInst ( PtrToIntInst I)
inlineinherited

Definition at line 191 of file InstVisitor.h.

191 { DELEGATE(CastInst);}
#define DELEGATE(CLASS_TO_VISIT)
Definition: InstVisitor.h:30
Instruction * llvm::InstVisitor< InstCombiner , Instruction * >::visitResumeInst ( ResumeInst I)
inlineinherited

Definition at line 170 of file InstVisitor.h.

170 { DELEGATE(TerminatorInst);}
#define DELEGATE(CLASS_TO_VISIT)
Definition: InstVisitor.h:30
Instruction * llvm::InstVisitor< InstCombiner , Instruction * >::visitReturnInst ( ReturnInst I)
inlineinherited

Definition at line 166 of file InstVisitor.h.

166 { DELEGATE(TerminatorInst);}
#define DELEGATE(CLASS_TO_VISIT)
Definition: InstVisitor.h:30
Instruction * InstCombiner::visitSDiv ( BinaryOperator I)

Definition at line 968 of file InstCombineMulDivRem.cpp.

968  {
969  Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
970 
971  if (Value *V = SimplifyVectorOp(I))
972  return ReplaceInstUsesWith(I, V);
973 
974  if (Value *V = SimplifySDivInst(Op0, Op1, DL))
975  return ReplaceInstUsesWith(I, V);
976 
977  // Handle the integer div common cases
978  if (Instruction *Common = commonIDivTransforms(I))
979  return Common;
980 
981  // sdiv X, -1 == -X
982  if (match(Op1, m_AllOnes()))
983  return BinaryOperator::CreateNeg(Op0);
984 
985  if (ConstantInt *RHS = dyn_cast<ConstantInt>(Op1)) {
986  // sdiv X, C --> ashr exact X, log2(C)
987  if (I.isExact() && RHS->getValue().isNonNegative() &&
988  RHS->getValue().isPowerOf2()) {
989  Value *ShAmt = llvm::ConstantInt::get(RHS->getType(),
990  RHS->getValue().exactLogBase2());
991  return BinaryOperator::CreateExactAShr(Op0, ShAmt, I.getName());
992  }
993  }
994 
995  if (Constant *RHS = dyn_cast<Constant>(Op1)) {
996  // X/INT_MIN -> X == INT_MIN
997  if (RHS->isMinSignedValue())
998  return new ZExtInst(Builder->CreateICmpEQ(Op0, Op1), I.getType());
999 
1000  // -X/C --> X/-C provided the negation doesn't overflow.
1001  if (SubOperator *Sub = dyn_cast<SubOperator>(Op0))
1002  if (match(Sub->getOperand(0), m_Zero()) && Sub->hasNoSignedWrap())
1003  return BinaryOperator::CreateSDiv(Sub->getOperand(1),
1004  ConstantExpr::getNeg(RHS));
1005  }
1006 
1007  // If the sign bits of both operands are zero (i.e. we can prove they are
1008  // unsigned inputs), turn this into a udiv.
1009  if (I.getType()->isIntegerTy()) {
1011  if (MaskedValueIsZero(Op0, Mask)) {
1012  if (MaskedValueIsZero(Op1, Mask)) {
1013  // X sdiv Y -> X udiv Y, iff X and Y don't have sign bit set
1014  return BinaryOperator::CreateUDiv(Op0, Op1, I.getName());
1015  }
1016 
1017  if (match(Op1, m_Shl(m_Power2(), m_Value()))) {
1018  // X sdiv (1 << Y) -> X udiv (1 << Y) ( -> X u>> Y)
1019  // Safe because the only negative value (1 << Y) can take on is
1020  // INT_MIN, and X sdiv INT_MIN == X udiv INT_MIN == 0 if X doesn't have
1021  // the sign bit set.
1022  return BinaryOperator::CreateUDiv(Op0, Op1, I.getName());
1023  }
1024  }
1025  }
1026 
1027  return nullptr;
1028 }
class_match< Value > m_Value()
m_Value() - Match an arbitrary value and ignore it.
Definition: PatternMatch.h:70
static APInt getSignBit(unsigned BitWidth)
Get the SignBit for a specific bit width.
Definition: APInt.h:441
match_zero m_Zero()
Definition: PatternMatch.h:137
This class represents zero extension of integer types.
Value * SimplifyVectorOp(BinaryOperator &Inst)
Makes transformation of binary operation specific for vector types.
Value * CreateICmpEQ(Value *LHS, Value *RHS, const Twine &Name="")
Definition: IRBuilder.h:1247
StringRef getName() const
Definition: Value.cpp:168
bool match(Val *V, const Pattern &P)
Definition: PatternMatch.h:42
Instruction * commonIDivTransforms(BinaryOperator &I)
Common integer divide transforms.
cst_pred_ty< is_power2 > m_Power2()
m_Power2() - Match an integer or vector power of 2.
Definition: PatternMatch.h:281
LLVM Constant Representation.
Definition: Constant.h:41
Instruction * ReplaceInstUsesWith(Instruction &I, Value *V)
Definition: InstCombine.h:282
cst_pred_ty< is_all_ones > m_AllOnes()
m_AllOnes() - Match an integer or vector with all bits set to true.
Definition: PatternMatch.h:265
BinaryOp_match< LHS, RHS, Instruction::Shl > m_Shl(const LHS &L, const RHS &R)
Definition: PatternMatch.h:485
Value * getOperand(unsigned i) const
Definition: User.h:90
BuilderTy * Builder
Definition: InstCombine.h:102
NUW NUW NUW NUW Exact static Exact BinaryOperator * CreateNeg(Value *Op, const Twine &Name="", Instruction *InsertBefore=nullptr)
Class for constant integers.
Definition: Constants.h:51
Type * getType() const
Definition: Value.h:215
static Constant * get(Type *Ty, uint64_t V, bool isSigned=false)
Definition: Constants.cpp:528
bool isExact() const
isExact - Determine whether the exact flag is set.
Class for arbitrary precision integers.
Definition: APInt.h:75
bool isIntegerTy() const
Definition: Type.h:193
static Constant * getNeg(Constant *C, bool HasNUW=false, bool HasNSW=false)
Definition: Constants.cpp:2083
bool MaskedValueIsZero(Value *V, const APInt &Mask, unsigned Depth=0) const
Definition: InstCombine.h:323
const DataLayout * DL
Definition: InstCombine.h:89
unsigned getPrimitiveSizeInBits() const LLVM_READONLY
Definition: Type.cpp:117
LLVM Value Representation.
Definition: Value.h:69
Value * SimplifySDivInst(Value *LHS, Value *RHS, const DataLayout *TD=nullptr, const TargetLibraryInfo *TLI=nullptr, const DominatorTree *DT=nullptr)
Instruction * InstCombiner::visitSelectInst ( SelectInst SI)

Definition at line 823 of file InstCombineSelect.cpp.

823  {
824  Value *CondVal = SI.getCondition();
825  Value *TrueVal = SI.getTrueValue();
826  Value *FalseVal = SI.getFalseValue();
827 
828  if (Value *V = SimplifySelectInst(CondVal, TrueVal, FalseVal, DL))
829  return ReplaceInstUsesWith(SI, V);
830 
831  if (SI.getType()->isIntegerTy(1)) {
832  if (ConstantInt *C = dyn_cast<ConstantInt>(TrueVal)) {
833  if (C->getZExtValue()) {
834  // Change: A = select B, true, C --> A = or B, C
835  return BinaryOperator::CreateOr(CondVal, FalseVal);
836  }
837  // Change: A = select B, false, C --> A = and !B, C
838  Value *NotCond = Builder->CreateNot(CondVal, "not."+CondVal->getName());
839  return BinaryOperator::CreateAnd(NotCond, FalseVal);
840  }
841  if (ConstantInt *C = dyn_cast<ConstantInt>(FalseVal)) {
842  if (C->getZExtValue() == false) {
843  // Change: A = select B, C, false --> A = and B, C
844  return BinaryOperator::CreateAnd(CondVal, TrueVal);
845  }
846  // Change: A = select B, C, true --> A = or !B, C
847  Value *NotCond = Builder->CreateNot(CondVal, "not."+CondVal->getName());
848  return BinaryOperator::CreateOr(NotCond, TrueVal);
849  }
850 
851  // select a, b, a -> a&b
852  // select a, a, b -> a|b
853  if (CondVal == TrueVal)
854  return BinaryOperator::CreateOr(CondVal, FalseVal);
855  if (CondVal == FalseVal)
856  return BinaryOperator::CreateAnd(CondVal, TrueVal);
857 
858  // select a, ~a, b -> (~a)&b
859  // select a, b, ~a -> (~a)|b
860  if (match(TrueVal, m_Not(m_Specific(CondVal))))
861  return BinaryOperator::CreateAnd(TrueVal, FalseVal);
862  if (match(FalseVal, m_Not(m_Specific(CondVal))))
863  return BinaryOperator::CreateOr(TrueVal, FalseVal);
864  }
865 
866  // Selecting between two integer constants?
867  if (ConstantInt *TrueValC = dyn_cast<ConstantInt>(TrueVal))
868  if (ConstantInt *FalseValC = dyn_cast<ConstantInt>(FalseVal)) {
869  // select C, 1, 0 -> zext C to int
870  if (FalseValC->isZero() && TrueValC->getValue() == 1)
871  return new ZExtInst(CondVal, SI.getType());
872 
873  // select C, -1, 0 -> sext C to int
874  if (FalseValC->isZero() && TrueValC->isAllOnesValue())
875  return new SExtInst(CondVal, SI.getType());
876 
877  // select C, 0, 1 -> zext !C to int
878  if (TrueValC->isZero() && FalseValC->getValue() == 1) {
879  Value *NotCond = Builder->CreateNot(CondVal, "not."+CondVal->getName());
880  return new ZExtInst(NotCond, SI.getType());
881  }
882 
883  // select C, 0, -1 -> sext !C to int
884  if (TrueValC->isZero() && FalseValC->isAllOnesValue()) {
885  Value *NotCond = Builder->CreateNot(CondVal, "not."+CondVal->getName());
886  return new SExtInst(NotCond, SI.getType());
887  }
888 
889  if (Value *V = foldSelectICmpAnd(SI, TrueValC, FalseValC, Builder))
890  return ReplaceInstUsesWith(SI, V);
891  }
892 
893  // See if we are selecting two values based on a comparison of the two values.
894  if (FCmpInst *FCI = dyn_cast<FCmpInst>(CondVal)) {
895  if (FCI->getOperand(0) == TrueVal && FCI->getOperand(1) == FalseVal) {
896  // Transform (X == Y) ? X : Y -> Y
897  if (FCI->getPredicate() == FCmpInst::FCMP_OEQ) {
898  // This is not safe in general for floating point:
899  // consider X== -0, Y== +0.
900  // It becomes safe if either operand is a nonzero constant.
901  ConstantFP *CFPt, *CFPf;
902  if (((CFPt = dyn_cast<ConstantFP>(TrueVal)) &&
903  !CFPt->getValueAPF().isZero()) ||
904  ((CFPf = dyn_cast<ConstantFP>(FalseVal)) &&
905  !CFPf->getValueAPF().isZero()))
906  return ReplaceInstUsesWith(SI, FalseVal);
907  }
908  // Transform (X une Y) ? X : Y -> X
909  if (FCI->getPredicate() == FCmpInst::FCMP_UNE) {
910  // This is not safe in general for floating point:
911  // consider X== -0, Y== +0.
912  // It becomes safe if either operand is a nonzero constant.
913  ConstantFP *CFPt, *CFPf;
914  if (((CFPt = dyn_cast<ConstantFP>(TrueVal)) &&
915  !CFPt->getValueAPF().isZero()) ||
916  ((CFPf = dyn_cast<ConstantFP>(FalseVal)) &&
917  !CFPf->getValueAPF().isZero()))
918  return ReplaceInstUsesWith(SI, TrueVal);
919  }
920  // NOTE: if we wanted to, this is where to detect MIN/MAX
921 
922  } else if (FCI->getOperand(0) == FalseVal && FCI->getOperand(1) == TrueVal){
923  // Transform (X == Y) ? Y : X -> X
924  if (FCI->getPredicate() == FCmpInst::FCMP_OEQ) {
925  // This is not safe in general for floating point:
926  // consider X== -0, Y== +0.
927  // It becomes safe if either operand is a nonzero constant.
928  ConstantFP *CFPt, *CFPf;
929  if (((CFPt = dyn_cast<ConstantFP>(TrueVal)) &&
930  !CFPt->getValueAPF().isZero()) ||
931  ((CFPf = dyn_cast<ConstantFP>(FalseVal)) &&
932  !CFPf->getValueAPF().isZero()))
933  return ReplaceInstUsesWith(SI, FalseVal);
934  }
935  // Transform (X une Y) ? Y : X -> Y
936  if (FCI->getPredicate() == FCmpInst::FCMP_UNE) {
937  // This is not safe in general for floating point:
938  // consider X== -0, Y== +0.
939  // It becomes safe if either operand is a nonzero constant.
940  ConstantFP *CFPt, *CFPf;
941  if (((CFPt = dyn_cast<ConstantFP>(TrueVal)) &&
942  !CFPt->getValueAPF().isZero()) ||
943  ((CFPf = dyn_cast<ConstantFP>(FalseVal)) &&
944  !CFPf->getValueAPF().isZero()))
945  return ReplaceInstUsesWith(SI, TrueVal);
946  }
947  // NOTE: if we wanted to, this is where to detect MIN/MAX
948  }
949  // NOTE: if we wanted to, this is where to detect ABS
950  }
951 
952  // See if we are selecting two values based on a comparison of the two values.
953  if (ICmpInst *ICI = dyn_cast<ICmpInst>(CondVal))
954  if (Instruction *Result = visitSelectInstWithICmp(SI, ICI))
955  return Result;
956 
957  if (Instruction *TI = dyn_cast<Instruction>(TrueVal))
958  if (Instruction *FI = dyn_cast<Instruction>(FalseVal))
959  if (TI->hasOneUse() && FI->hasOneUse()) {
960  Instruction *AddOp = nullptr, *SubOp = nullptr;
961 
962  // Turn (select C, (op X, Y), (op X, Z)) -> (op X, (select C, Y, Z))
963  if (TI->getOpcode() == FI->getOpcode())
964  if (Instruction *IV = FoldSelectOpOp(SI, TI, FI))
965  return IV;
966 
967  // Turn select C, (X+Y), (X-Y) --> (X+(select C, Y, (-Y))). This is
968  // even legal for FP.
969  if ((TI->getOpcode() == Instruction::Sub &&
970  FI->getOpcode() == Instruction::Add) ||
971  (TI->getOpcode() == Instruction::FSub &&
972  FI->getOpcode() == Instruction::FAdd)) {
973  AddOp = FI; SubOp = TI;
974  } else if ((FI->getOpcode() == Instruction::Sub &&
975  TI->getOpcode() == Instruction::Add) ||
976  (FI->getOpcode() == Instruction::FSub &&
977  TI->getOpcode() == Instruction::FAdd)) {
978  AddOp = TI; SubOp = FI;
979  }
980 
981  if (AddOp) {
982  Value *OtherAddOp = nullptr;
983  if (SubOp->getOperand(0) == AddOp->getOperand(0)) {
984  OtherAddOp = AddOp->getOperand(1);
985  } else if (SubOp->getOperand(0) == AddOp->getOperand(1)) {
986  OtherAddOp = AddOp->getOperand(0);
987  }
988 
989  if (OtherAddOp) {
990  // So at this point we know we have (Y -> OtherAddOp):
991  // select C, (add X, Y), (sub X, Z)
992  Value *NegVal; // Compute -Z
993  if (SI.getType()->isFPOrFPVectorTy()) {
994  NegVal = Builder->CreateFNeg(SubOp->getOperand(1));
995  if (Instruction *NegInst = dyn_cast<Instruction>(NegVal)) {
997  Flags &= SubOp->getFastMathFlags();
998  NegInst->setFastMathFlags(Flags);
999  }
1000  } else {
1001  NegVal = Builder->CreateNeg(SubOp->getOperand(1));
1002  }
1003 
1004  Value *NewTrueOp = OtherAddOp;
1005  Value *NewFalseOp = NegVal;
1006  if (AddOp != TI)
1007  std::swap(NewTrueOp, NewFalseOp);
1008  Value *NewSel =
1009  Builder->CreateSelect(CondVal, NewTrueOp,
1010  NewFalseOp, SI.getName() + ".p");
1011 
1012  if (SI.getType()->isFPOrFPVectorTy()) {
1013  Instruction *RI =
1014  BinaryOperator::CreateFAdd(SubOp->getOperand(0), NewSel);
1015 
1016  FastMathFlags Flags = AddOp->getFastMathFlags();
1017  Flags &= SubOp->getFastMathFlags();
1018  RI->setFastMathFlags(Flags);
1019  return RI;
1020  } else
1021  return BinaryOperator::CreateAdd(SubOp->getOperand(0), NewSel);
1022  }
1023  }
1024  }
1025 
1026  // See if we can fold the select into one of our operands.
1027  if (SI.getType()->isIntegerTy()) {
1028  if (Instruction *FoldI = FoldSelectIntoOp(SI, TrueVal, FalseVal))
1029  return FoldI;
1030 
1031  // MAX(MAX(a, b), a) -> MAX(a, b)
1032  // MIN(MIN(a, b), a) -> MIN(a, b)
1033  // MAX(MIN(a, b), a) -> a
1034  // MIN(MAX(a, b), a) -> a
1035  Value *LHS, *RHS, *LHS2, *RHS2;
1036  if (SelectPatternFlavor SPF = MatchSelectPattern(&SI, LHS, RHS)) {
1037  if (SelectPatternFlavor SPF2 = MatchSelectPattern(LHS, LHS2, RHS2))
1038  if (Instruction *R = FoldSPFofSPF(cast<Instruction>(LHS),SPF2,LHS2,RHS2,
1039  SI, SPF, RHS))
1040  return R;
1041  if (SelectPatternFlavor SPF2 = MatchSelectPattern(RHS, LHS2, RHS2))
1042  if (Instruction *R = FoldSPFofSPF(cast<Instruction>(RHS),SPF2,LHS2,RHS2,
1043  SI, SPF, LHS))
1044  return R;
1045  }
1046 
1047  // TODO.
1048  // ABS(-X) -> ABS(X)
1049  }
1050 
1051  // See if we can fold the select into a phi node if the condition is a select.
1052  if (isa<PHINode>(SI.getCondition()))
1053  // The true/false values have to be live in the PHI predecessor's blocks.
1054  if (CanSelectOperandBeMappingIntoPredBlock(TrueVal, SI) &&
1056  if (Instruction *NV = FoldOpIntoPhi(SI))
1057  return NV;
1058 
1059  if (SelectInst *TrueSI = dyn_cast<SelectInst>(TrueVal)) {
1060  if (TrueSI->getCondition() == CondVal) {
1061  if (SI.getTrueValue() == TrueSI->getTrueValue())
1062  return nullptr;
1063  SI.setOperand(1, TrueSI->getTrueValue());
1064  return &SI;
1065  }
1066  }
1067  if (SelectInst *FalseSI = dyn_cast<SelectInst>(FalseVal)) {
1068  if (FalseSI->getCondition() == CondVal) {
1069  if (SI.getFalseValue() == FalseSI->getFalseValue())
1070  return nullptr;
1071  SI.setOperand(2, FalseSI->getFalseValue());
1072  return &SI;
1073  }
1074  }
1075 
1076  if (BinaryOperator::isNot(CondVal)) {
1078  SI.setOperand(1, FalseVal);
1079  SI.setOperand(2, TrueVal);
1080  return &SI;
1081  }
1082 
1083  if (VectorType* VecTy = dyn_cast<VectorType>(SI.getType())) {
1084  unsigned VWidth = VecTy->getNumElements();
1085  APInt UndefElts(VWidth, 0);
1086  APInt AllOnesEltMask(APInt::getAllOnesValue(VWidth));
1087  if (Value *V = SimplifyDemandedVectorElts(&SI, AllOnesEltMask, UndefElts)) {
1088  if (V != &SI)
1089  return ReplaceInstUsesWith(SI, V);
1090  return &SI;
1091  }
1092 
1093  if (isa<ConstantAggregateZero>(CondVal)) {
1094  return ReplaceInstUsesWith(SI, FalseVal);
1095  }
1096  }
1097 
1098  return nullptr;
1099 }
Instruction * FoldOpIntoPhi(Instruction &I)
void setFastMathFlags(FastMathFlags FMF)
static APInt getAllOnesValue(unsigned numBits)
Get the all-ones value.
Definition: APInt.h:448
This class represents zero extension of integer types.
Value * SimplifyDemandedVectorElts(Value *V, APInt DemandedElts, APInt &UndefElts, unsigned Depth=0)
1 1 1 0 True if unordered or not equal
Definition: InstrTypes.h:688
FastMathFlags getFastMathFlags() const
This class represents a sign extension of integer types.
StringRef getName() const
Definition: Value.cpp:168
bool match(Val *V, const Pattern &P)
Definition: PatternMatch.h:42
static Value * foldSelectICmpAnd(const SelectInst &SI, ConstantInt *TrueVal, ConstantInt *FalseVal, InstCombiner::BuilderTy *Builder)
not_match< LHS > m_Not(const LHS &L)
Definition: PatternMatch.h:828
Represents a floating point comparison operator.
static const Value * getNotArgument(const Value *BinOp)
Value * CreateNot(Value *V, const Twine &Name="")
Definition: IRBuilder.h:885
const Value * getCondition() const
Instruction * ReplaceInstUsesWith(Instruction &I, Value *V)
Definition: InstCombine.h:282
specificval_ty m_Specific(const Value *V)
m_Specific - Match if we have a specific specified value.
Definition: PatternMatch.h:323
Value * SimplifySelectInst(Value *Cond, Value *TrueVal, Value *FalseVal, const DataLayout *TD=nullptr, const TargetLibraryInfo *TLI=nullptr, const DominatorTree *DT=nullptr)
Represent an integer comparison operator.
Definition: Instructions.h:977
Instruction * FoldSelectIntoOp(SelectInst &SI, Value *, Value *)
Value * getOperand(unsigned i) const
Definition: User.h:90
static bool isNot(const Value *V)
Instruction * FoldSelectOpOp(SelectInst &SI, Instruction *TI, Instruction *FI)
BuilderTy * Builder
Definition: InstCombine.h:102
bool isFPOrFPVectorTy() const
Definition: Type.h:183
const Value * getTrueValue() const
static SelectPatternFlavor MatchSelectPattern(Value *V, Value *&LHS, Value *&RHS)
Class for constant integers.
Definition: Constants.h:51
Type * getType() const
Definition: Value.h:215
SelectPatternFlavor
Definition: InstCombine.h:35
void setOperand(unsigned i, Value *Val)
Definition: User.h:94
Instruction * FoldSPFofSPF(Instruction *Inner, SelectPatternFlavor SPF1, Value *A, Value *B, Instruction &Outer, SelectPatternFlavor SPF2, Value *C)
void swap(llvm::BitVector &LHS, llvm::BitVector &RHS)
Implement std::swap in terms of BitVector swap.
Definition: BitVector.h:590
Class for arbitrary precision integers.
Definition: APInt.h:75
bool isIntegerTy() const
Definition: Type.h:193
Value * CreateSelect(Value *C, Value *True, Value *False, const Twine &Name="")
Definition: IRBuilder.h:1377
static bool CanSelectOperandBeMappingIntoPredBlock(const Value *V, const SelectInst &SI)
const APFloat & getValueAPF() const
Definition: Constants.h:263
const DataLayout * DL
Definition: InstCombine.h:89
Value * CreateNeg(Value *V, const Twine &Name="", bool HasNUW=false, bool HasNSW=false)
Definition: IRBuilder.h:863
0 0 0 1 True if ordered and equal
Definition: InstrTypes.h:675
LLVM Value Representation.
Definition: Value.h:69
bool isZero() const
Returns true if and only if the float is plus or minus zero.
Definition: APFloat.h:377
const Value * getFalseValue() const
Convenience struct for specifying and reasoning about fast-math flags.
Definition: Operator.h:170
Instruction * visitSelectInstWithICmp(SelectInst &SI, ICmpInst *ICI)
Value * CreateFNeg(Value *V, const Twine &Name="", MDNode *FPMathTag=nullptr)
Definition: IRBuilder.h:878
Instruction * InstCombiner::visitSelectInstWithICmp ( SelectInst SI,
ICmpInst ICI 
)

visitSelectInstWithICmp - Visit a SelectInst that has an ICmpInst as its first operand.

Definition at line 469 of file InstCombineSelect.cpp.

470  {
471  bool Changed = false;
472  ICmpInst::Predicate Pred = ICI->getPredicate();
473  Value *CmpLHS = ICI->getOperand(0);
474  Value *CmpRHS = ICI->getOperand(1);
475  Value *TrueVal = SI.getTrueValue();
476  Value *FalseVal = SI.getFalseValue();
477 
478  // Check cases where the comparison is with a constant that
479  // can be adjusted to fit the min/max idiom. We may move or edit ICI
480  // here, so make sure the select is the only user.
481  if (ICI->hasOneUse())
482  if (ConstantInt *CI = dyn_cast<ConstantInt>(CmpRHS)) {
483  // X < MIN ? T : F --> F
484  if ((Pred == ICmpInst::ICMP_SLT || Pred == ICmpInst::ICMP_ULT)
485  && CI->isMinValue(Pred == ICmpInst::ICMP_SLT))
486  return ReplaceInstUsesWith(SI, FalseVal);
487  // X > MAX ? T : F --> F
488  else if ((Pred == ICmpInst::ICMP_SGT || Pred == ICmpInst::ICMP_UGT)
489  && CI->isMaxValue(Pred == ICmpInst::ICMP_SGT))
490  return ReplaceInstUsesWith(SI, FalseVal);
491  switch (Pred) {
492  default: break;
493  case ICmpInst::ICMP_ULT:
494  case ICmpInst::ICMP_SLT:
495  case ICmpInst::ICMP_UGT:
496  case ICmpInst::ICMP_SGT: {
497  // These transformations only work for selects over integers.
498  IntegerType *SelectTy = dyn_cast<IntegerType>(SI.getType());
499  if (!SelectTy)
500  break;
501 
502  Constant *AdjustedRHS;
503  if (Pred == ICmpInst::ICMP_UGT || Pred == ICmpInst::ICMP_SGT)
504  AdjustedRHS = ConstantInt::get(CI->getContext(), CI->getValue() + 1);
505  else // (Pred == ICmpInst::ICMP_ULT || Pred == ICmpInst::ICMP_SLT)
506  AdjustedRHS = ConstantInt::get(CI->getContext(), CI->getValue() - 1);
507 
508  // X > C ? X : C+1 --> X < C+1 ? C+1 : X
509  // X < C ? X : C-1 --> X > C-1 ? C-1 : X
510  if ((CmpLHS == TrueVal && AdjustedRHS == FalseVal) ||
511  (CmpLHS == FalseVal && AdjustedRHS == TrueVal))
512  ; // Nothing to do here. Values match without any sign/zero extension.
513 
514  // Types do not match. Instead of calculating this with mixed types
515  // promote all to the larger type. This enables scalar evolution to
516  // analyze this expression.
517  else if (CmpRHS->getType()->getScalarSizeInBits()
518  < SelectTy->getBitWidth()) {
519  Constant *sextRHS = ConstantExpr::getSExt(AdjustedRHS, SelectTy);
520 
521  // X = sext x; x >s c ? X : C+1 --> X = sext x; X <s C+1 ? C+1 : X
522  // X = sext x; x <s c ? X : C-1 --> X = sext x; X >s C-1 ? C-1 : X
523  // X = sext x; x >u c ? X : C+1 --> X = sext x; X <u C+1 ? C+1 : X
524  // X = sext x; x <u c ? X : C-1 --> X = sext x; X >u C-1 ? C-1 : X
525  if (match(TrueVal, m_SExt(m_Specific(CmpLHS))) &&
526  sextRHS == FalseVal) {
527  CmpLHS = TrueVal;
528  AdjustedRHS = sextRHS;
529  } else if (match(FalseVal, m_SExt(m_Specific(CmpLHS))) &&
530  sextRHS == TrueVal) {
531  CmpLHS = FalseVal;
532  AdjustedRHS = sextRHS;
533  } else if (ICI->isUnsigned()) {
534  Constant *zextRHS = ConstantExpr::getZExt(AdjustedRHS, SelectTy);
535  // X = zext x; x >u c ? X : C+1 --> X = zext x; X <u C+1 ? C+1 : X
536  // X = zext x; x <u c ? X : C-1 --> X = zext x; X >u C-1 ? C-1 : X
537  // zext + signed compare cannot be changed:
538  // 0xff <s 0x00, but 0x00ff >s 0x0000
539  if (match(TrueVal, m_ZExt(m_Specific(CmpLHS))) &&
540  zextRHS == FalseVal) {
541  CmpLHS = TrueVal;
542  AdjustedRHS = zextRHS;
543  } else if (match(FalseVal, m_ZExt(m_Specific(CmpLHS))) &&
544  zextRHS == TrueVal) {
545  CmpLHS = FalseVal;
546  AdjustedRHS = zextRHS;
547  } else
548  break;
549  } else
550  break;
551  } else
552  break;
553 
554  Pred = ICmpInst::getSwappedPredicate(Pred);
555  CmpRHS = AdjustedRHS;
556  std::swap(FalseVal, TrueVal);
557  ICI->setPredicate(Pred);
558  ICI->setOperand(0, CmpLHS);
559  ICI->setOperand(1, CmpRHS);
560  SI.setOperand(1, TrueVal);
561  SI.setOperand(2, FalseVal);
562 
563  // Move ICI instruction right before the select instruction. Otherwise
564  // the sext/zext value may be defined after the ICI instruction uses it.
565  ICI->moveBefore(&SI);
566 
567  Changed = true;
568  break;
569  }
570  }
571  }
572 
573  // Transform (X >s -1) ? C1 : C2 --> ((X >>s 31) & (C2 - C1)) + C1
574  // and (X <s 0) ? C2 : C1 --> ((X >>s 31) & (C2 - C1)) + C1
575  // FIXME: Type and constness constraints could be lifted, but we have to
576  // watch code size carefully. We should consider xor instead of
577  // sub/add when we decide to do that.
578  if (IntegerType *Ty = dyn_cast<IntegerType>(CmpLHS->getType())) {
579  if (TrueVal->getType() == Ty) {
580  if (ConstantInt *Cmp = dyn_cast<ConstantInt>(CmpRHS)) {
581  ConstantInt *C1 = nullptr, *C2 = nullptr;
582  if (Pred == ICmpInst::ICMP_SGT && Cmp->isAllOnesValue()) {
583  C1 = dyn_cast<ConstantInt>(TrueVal);
584  C2 = dyn_cast<ConstantInt>(FalseVal);
585  } else if (Pred == ICmpInst::ICMP_SLT && Cmp->isNullValue()) {
586  C1 = dyn_cast<ConstantInt>(FalseVal);
587  C2 = dyn_cast<ConstantInt>(TrueVal);
588  }
589  if (C1 && C2) {
590  // This shift results in either -1 or 0.
591  Value *AShr = Builder->CreateAShr(CmpLHS, Ty->getBitWidth()-1);
592 
593  // Check if we can express the operation with a single or.
594  if (C2->isAllOnesValue())
595  return ReplaceInstUsesWith(SI, Builder->CreateOr(AShr, C1));
596 
597  Value *And = Builder->CreateAnd(AShr, C2->getValue()-C1->getValue());
598  return ReplaceInstUsesWith(SI, Builder->CreateAdd(And, C1));
599  }
600  }
601  }
602  }
603 
604  // If we have an equality comparison then we know the value in one of the
605  // arms of the select. See if substituting this value into the arm and
606  // simplifying the result yields the same value as the other arm.
607  if (Pred == ICmpInst::ICMP_EQ) {
608  if (SimplifyWithOpReplaced(FalseVal, CmpLHS, CmpRHS, DL, TLI) == TrueVal ||
609  SimplifyWithOpReplaced(FalseVal, CmpRHS, CmpLHS, DL, TLI) == TrueVal)
610  return ReplaceInstUsesWith(SI, FalseVal);
611  if (SimplifyWithOpReplaced(TrueVal, CmpLHS, CmpRHS, DL, TLI) == FalseVal ||
612  SimplifyWithOpReplaced(TrueVal, CmpRHS, CmpLHS, DL, TLI) == FalseVal)
613  return ReplaceInstUsesWith(SI, FalseVal);
614  } else if (Pred == ICmpInst::ICMP_NE) {
615  if (SimplifyWithOpReplaced(TrueVal, CmpLHS, CmpRHS, DL, TLI) == FalseVal ||
616  SimplifyWithOpReplaced(TrueVal, CmpRHS, CmpLHS, DL, TLI) == FalseVal)
617  return ReplaceInstUsesWith(SI, TrueVal);
618  if (SimplifyWithOpReplaced(FalseVal, CmpLHS, CmpRHS, DL, TLI) == TrueVal ||
619  SimplifyWithOpReplaced(FalseVal, CmpRHS, CmpLHS, DL, TLI) == TrueVal)
620  return ReplaceInstUsesWith(SI, TrueVal);
621  }
622 
623  // NOTE: if we wanted to, this is where to detect integer MIN/MAX
624 
625  if (CmpRHS != CmpLHS && isa<Constant>(CmpRHS)) {
626  if (CmpLHS == TrueVal && Pred == ICmpInst::ICMP_EQ) {
627  // Transform (X == C) ? X : Y -> (X == C) ? C : Y
628  SI.setOperand(1, CmpRHS);
629  Changed = true;
630  } else if (CmpLHS == FalseVal && Pred == ICmpInst::ICMP_NE) {
631  // Transform (X != C) ? Y : X -> (X != C) ? Y : C
632  SI.setOperand(2, CmpRHS);
633  Changed = true;
634  }
635  }
636 
637  if (Value *V = foldSelectICmpAndOr(SI, TrueVal, FalseVal, Builder))
638  return ReplaceInstUsesWith(SI, V);
639 
640  return Changed ? &SI : nullptr;
641 }
unsigned less than
Definition: InstrTypes.h:697
unsigned getBitWidth() const
Get the number of bits in this IntegerType.
Definition: DerivedTypes.h:61
bool match(Val *V, const Pattern &P)
Definition: PatternMatch.h:42
TargetLibraryInfo * TLI
Definition: InstCombine.h:90
const APInt & getValue() const
Return the constant's value.
Definition: Constants.h:105
static Value * foldSelectICmpAndOr(SelectInst &SI, Value *TrueVal, Value *FalseVal, InstCombiner::BuilderTy *Builder)
Value * CreateAnd(Value *LHS, Value *RHS, const Twine &Name="")
Definition: IRBuilder.h:806
static Value * SimplifyWithOpReplaced(Value *V, Value *Op, Value *RepOp, const DataLayout *TD, const TargetLibraryInfo *TLI)
Value * CreateOr(Value *LHS, Value *RHS, const Twine &Name="")
Definition: IRBuilder.h:822
CastClass_match< OpTy, Instruction::ZExt > m_ZExt(const OpTy &Op)
m_ZExt
Definition: PatternMatch.h:782
Value * CreateAShr(Value *LHS, Value *RHS, const Twine &Name="", bool isExact=false)
Definition: IRBuilder.h:788
Value * CreateAdd(Value *LHS, Value *RHS, const Twine &Name="", bool HasNUW=false, bool HasNSW=false)
Definition: IRBuilder.h:632
LLVM Constant Representation.
Definition: Constant.h:41
Instruction * ReplaceInstUsesWith(Instruction &I, Value *V)
Definition: InstCombine.h:282
specificval_ty m_Specific(const Value *V)
m_Specific - Match if we have a specific specified value.
Definition: PatternMatch.h:323
Value * getOperand(unsigned i) const
Definition: User.h:90
Integer representation type.
Definition: DerivedTypes.h:37
Predicate getPredicate() const
Return the predicate for this instruction.
Definition: InstrTypes.h:735
BuilderTy * Builder
Definition: InstCombine.h:102
const Value * getTrueValue() const
signed greater than
Definition: InstrTypes.h:699
CastClass_match< OpTy, Instruction::SExt > m_SExt(const OpTy &Op)
m_SExt
Definition: PatternMatch.h:775
Class for constant integers.
Definition: Constants.h:51
unsigned getScalarSizeInBits() const LLVM_READONLY
Definition: Type.cpp:135
Type * getType() const
Definition: Value.h:215
signed less than
Definition: InstrTypes.h:701
Predicate getSwappedPredicate() const
Return the predicate as if the operands were swapped.
Definition: InstrTypes.h:774
static Constant * get(Type *Ty, uint64_t V, bool isSigned=false)
Definition: Constants.cpp:528
void setPredicate(Predicate P)
Set the predicate for this instruction to the specified value.
Definition: InstrTypes.h:740
void setOperand(unsigned i, Value *Val)
Definition: User.h:94
void swap(llvm::BitVector &LHS, llvm::BitVector &RHS)
Implement std::swap in terms of BitVector swap.
Definition: BitVector.h:590
LLVM_ATTRIBUTE_UNUSED_RESULT std::enable_if< !is_simple_type< Y >::value, typename cast_retty< X, const Y >::ret_type >::type dyn_cast(const Y &Val)
Definition: Casting.h:265
APInt And(const APInt &LHS, const APInt &RHS)
Bitwise AND function for APInt.
Definition: APInt.h:1865
static Constant * getSExt(Constant *C, Type *Ty)
Definition: Constants.cpp:1601
static Constant * getZExt(Constant *C, Type *Ty)
Definition: Constants.cpp:1615
bool hasOneUse() const
Definition: Value.h:284
bool isUnsigned() const
Determine if this instruction is using an unsigned comparison.
Definition: InstrTypes.h:807
const DataLayout * DL
Definition: InstCombine.h:89
LLVM Value Representation.
Definition: Value.h:69
void moveBefore(Instruction *MovePos)
Definition: Instruction.cpp:95
const Value * getFalseValue() const
unsigned greater than
Definition: InstrTypes.h:695
Instruction * InstCombiner::visitSExt ( SExtInst CI)

Definition at line 1041 of file InstCombineCasts.cpp.

1041  {
1042  // If this sign extend is only used by a truncate, let the truncate be
1043  // eliminated before we try to optimize this sext.
1044  if (CI.hasOneUse() && isa<TruncInst>(CI.user_back()))
1045  return nullptr;
1046 
1047  if (Instruction *I = commonCastTransforms(CI))
1048  return I;
1049 
1050  // See if we can simplify any instructions used by the input whose sole
1051  // purpose is to compute bits we don't care about.
1053  return &CI;
1054 
1055  Value *Src = CI.getOperand(0);
1056  Type *SrcTy = Src->getType(), *DestTy = CI.getType();
1057 
1058  // Attempt to extend the entire input expression tree to the destination
1059  // type. Only do this if the dest type is a simple type, don't convert the
1060  // expression tree to something weird like i93 unless the source is also
1061  // strange.
1062  if ((DestTy->isVectorTy() || ShouldChangeType(SrcTy, DestTy)) &&
1063  CanEvaluateSExtd(Src, DestTy)) {
1064  // Okay, we can transform this! Insert the new expression now.
1065  DEBUG(dbgs() << "ICE: EvaluateInDifferentType converting expression type"
1066  " to avoid sign extend: " << CI);
1067  Value *Res = EvaluateInDifferentType(Src, DestTy, true);
1068  assert(Res->getType() == DestTy);
1069 
1070  uint32_t SrcBitSize = SrcTy->getScalarSizeInBits();
1071  uint32_t DestBitSize = DestTy->getScalarSizeInBits();
1072 
1073  // If the high bits are already filled with sign bit, just replace this
1074  // cast with the result.
1075  if (ComputeNumSignBits(Res) > DestBitSize - SrcBitSize)
1076  return ReplaceInstUsesWith(CI, Res);
1077 
1078  // We need to emit a shl + ashr to do the sign extend.
1079  Value *ShAmt = ConstantInt::get(DestTy, DestBitSize-SrcBitSize);
1080  return BinaryOperator::CreateAShr(Builder->CreateShl(Res, ShAmt, "sext"),
1081  ShAmt);
1082  }
1083 
1084  // If this input is a trunc from our destination, then turn sext(trunc(x))
1085  // into shifts.
1086  if (TruncInst *TI = dyn_cast<TruncInst>(Src))
1087  if (TI->hasOneUse() && TI->getOperand(0)->getType() == DestTy) {
1088  uint32_t SrcBitSize = SrcTy->getScalarSizeInBits();
1089  uint32_t DestBitSize = DestTy->getScalarSizeInBits();
1090 
1091  // We need to emit a shl + ashr to do the sign extend.
1092  Value *ShAmt = ConstantInt::get(DestTy, DestBitSize-SrcBitSize);
1093  Value *Res = Builder->CreateShl(TI->getOperand(0), ShAmt, "sext");
1094  return BinaryOperator::CreateAShr(Res, ShAmt);
1095  }
1096 
1097  if (ICmpInst *ICI = dyn_cast<ICmpInst>(Src))
1098  return transformSExtICmp(ICI, CI);
1099 
1100  // If the input is a shl/ashr pair of a same constant, then this is a sign
1101  // extension from a smaller value. If we could trust arbitrary bitwidth
1102  // integers, we could turn this into a truncate to the smaller bit and then
1103  // use a sext for the whole extension. Since we don't, look deeper and check
1104  // for a truncate. If the source and dest are the same type, eliminate the
1105  // trunc and extend and just do shifts. For example, turn:
1106  // %a = trunc i32 %i to i8
1107  // %b = shl i8 %a, 6
1108  // %c = ashr i8 %b, 6
1109  // %d = sext i8 %c to i32
1110  // into:
1111  // %a = shl i32 %i, 30
1112  // %d = ashr i32 %a, 30
1113  Value *A = nullptr;
1114  // TODO: Eventually this could be subsumed by EvaluateInDifferentType.
1115  ConstantInt *BA = nullptr, *CA = nullptr;
1116  if (match(Src, m_AShr(m_Shl(m_Trunc(m_Value(A)), m_ConstantInt(BA)),
1117  m_ConstantInt(CA))) &&
1118  BA == CA && A->getType() == CI.getType()) {
1119  unsigned MidSize = Src->getType()->getScalarSizeInBits();
1120  unsigned SrcDstSize = CI.getType()->getScalarSizeInBits();
1121  unsigned ShAmt = CA->getZExtValue()+SrcDstSize-MidSize;
1122  Constant *ShAmtV = ConstantInt::get(CI.getType(), ShAmt);
1123  A = Builder->CreateShl(A, ShAmtV, CI.getName());
1124  return BinaryOperator::CreateAShr(A, ShAmtV);
1125  }
1126 
1127  return nullptr;
1128 }
static bool CanEvaluateSExtd(Value *V, Type *Ty)
class_match< Value > m_Value()
m_Value() - Match an arbitrary value and ignore it.
Definition: PatternMatch.h:70
Instruction * commonCastTransforms(CastInst &CI)
Implement the transforms common to all CastInst visitors.
BinaryOp_match< LHS, RHS, Instruction::AShr > m_AShr(const LHS &L, const RHS &R)
Definition: PatternMatch.h:497
Instruction * transformSExtICmp(ICmpInst *ICI, Instruction &CI)
StringRef getName() const
Definition: Value.cpp:168
bool match(Val *V, const Pattern &P)
Definition: PatternMatch.h:42
unsigned ComputeNumSignBits(Value *Op, unsigned Depth=0) const
Definition: InstCombine.h:327
CastClass_match< OpTy, Instruction::Trunc > m_Trunc(const OpTy &Op)
m_Trunc
Definition: PatternMatch.h:768
class_match< ConstantInt > m_ConstantInt()
m_ConstantInt() - Match an arbitrary ConstantInt and ignore it.
Definition: PatternMatch.h:72
This class represents a truncation of integer types.
Value * EvaluateInDifferentType(Value *V, Type *Ty, bool isSigned)
assert(Globals.size() > 1)
LLVM Constant Representation.
Definition: Constant.h:41
Instruction * ReplaceInstUsesWith(Instruction &I, Value *V)
Definition: InstCombine.h:282
BinaryOp_match< LHS, RHS, Instruction::Shl > m_Shl(const LHS &L, const RHS &R)
Definition: PatternMatch.h:485
Represent an integer comparison operator.
Definition: Instructions.h:977
Value * getOperand(unsigned i) const
Definition: User.h:90
#define DEBUG(X)
BuilderTy * Builder
Definition: InstCombine.h:102
Definition: test.h:1
Class for constant integers.
Definition: Constants.h:51
unsigned getScalarSizeInBits() const LLVM_READONLY
Definition: Type.cpp:135
Type * getType() const
Definition: Value.h:215
Instruction * user_back()
Definition: Instruction.h:50
static Constant * get(Type *Ty, uint64_t V, bool isSigned=false)
Definition: Constants.cpp:528
raw_ostream & dbgs()
dbgs - Return a circular-buffered debug stream.
Value * CreateShl(Value *LHS, Value *RHS, const Twine &Name="", bool HasNUW=false, bool HasNSW=false)
Definition: IRBuilder.h:751
bool ShouldChangeType(Type *From, Type *To) const
#define I(x, y, z)
Definition: MD5.cpp:54
bool hasOneUse() const
Definition: Value.h:284
LLVM Value Representation.
Definition: Value.h:69
bool SimplifyDemandedInstructionBits(Instruction &Inst)
Instruction * llvm::InstVisitor< InstCombiner , Instruction * >::visitSExtInst ( SExtInst I)
inlineinherited

Definition at line 184 of file InstVisitor.h.

184 { DELEGATE(CastInst);}
#define DELEGATE(CLASS_TO_VISIT)
Definition: InstVisitor.h:30
Instruction * InstCombiner::visitShl ( BinaryOperator I)

Definition at line 688 of file InstCombineShifts.cpp.

688  {
689  if (Value *V = SimplifyVectorOp(I))
690  return ReplaceInstUsesWith(I, V);
691 
692  if (Value *V = SimplifyShlInst(I.getOperand(0), I.getOperand(1),
694  DL))
695  return ReplaceInstUsesWith(I, V);
696 
698  return V;
699 
700  if (ConstantInt *Op1C = dyn_cast<ConstantInt>(I.getOperand(1))) {
701  unsigned ShAmt = Op1C->getZExtValue();
702 
703  // If the shifted-out value is known-zero, then this is a NUW shift.
704  if (!I.hasNoUnsignedWrap() &&
706  APInt::getHighBitsSet(Op1C->getBitWidth(), ShAmt))) {
708  return &I;
709  }
710 
711  // If the shifted out value is all signbits, this is a NSW shift.
712  if (!I.hasNoSignedWrap() &&
713  ComputeNumSignBits(I.getOperand(0)) > ShAmt) {
714  I.setHasNoSignedWrap();
715  return &I;
716  }
717  }
718 
719  // (C1 << A) << C2 -> (C1 << C2) << A
720  Constant *C1, *C2;
721  Value *A;
722  if (match(I.getOperand(0), m_OneUse(m_Shl(m_Constant(C1), m_Value(A)))) &&
723  match(I.getOperand(1), m_Constant(C2)))
724  return BinaryOperator::CreateShl(ConstantExpr::getShl(C1, C2), A);
725 
726  return nullptr;
727 }
class_match< Value > m_Value()
m_Value() - Match an arbitrary value and ignore it.
Definition: PatternMatch.h:70
void setHasNoSignedWrap(bool b=true)
Value * SimplifyVectorOp(BinaryOperator &Inst)
Makes transformation of binary operation specific for vector types.
class_match< Constant > m_Constant()
Definition: PatternMatch.h:78
bool match(Val *V, const Pattern &P)
Definition: PatternMatch.h:42
unsigned ComputeNumSignBits(Value *Op, unsigned Depth=0) const
Definition: InstCombine.h:327
Instruction * commonShiftTransforms(BinaryOperator &I)
void setHasNoUnsignedWrap(bool b=true)
static APInt getHighBitsSet(unsigned numBits, unsigned hiBitsSet)
Get a value with high bits set.
Definition: APInt.h:508
OneUse_match< T > m_OneUse(const T &SubPattern)
Definition: PatternMatch.h:60
LLVM Constant Representation.
Definition: Constant.h:41
Instruction * ReplaceInstUsesWith(Instruction &I, Value *V)
Definition: InstCombine.h:282
BinaryOp_match< LHS, RHS, Instruction::Shl > m_Shl(const LHS &L, const RHS &R)
Definition: PatternMatch.h:485
Value * getOperand(unsigned i) const
Definition: User.h:90
bool hasNoSignedWrap() const
hasNoSignedWrap - Determine whether the no signed wrap flag is set.
Class for constant integers.
Definition: Constants.h:51
Value * SimplifyShlInst(Value *Op0, Value *Op1, bool isNSW, bool isNUW, const DataLayout *TD=nullptr, const TargetLibraryInfo *TLI=nullptr, const DominatorTree *DT=nullptr)
#define I(x, y, z)
Definition: MD5.cpp:54
static Constant * getShl(Constant *C1, Constant *C2, bool HasNUW=false, bool HasNSW=false)
Definition: Constants.cpp:2173
bool MaskedValueIsZero(Value *V, const APInt &Mask, unsigned Depth=0) const
Definition: InstCombine.h:323
const DataLayout * DL
Definition: InstCombine.h:89
LLVM Value Representation.
Definition: Value.h:69
bool hasNoUnsignedWrap() const
hasNoUnsignedWrap - Determine whether the no unsigned wrap flag is set.
Instruction * InstCombiner::visitShuffleVectorInst ( ShuffleVectorInst SVI)

Definition at line 856 of file InstCombineVectorOps.cpp.

856  {
857  Value *LHS = SVI.getOperand(0);
858  Value *RHS = SVI.getOperand(1);
860 
861  bool MadeChange = false;
862 
863  // Undefined shuffle mask -> undefined value.
864  if (isa<UndefValue>(SVI.getOperand(2)))
865  return ReplaceInstUsesWith(SVI, UndefValue::get(SVI.getType()));
866 
867  unsigned VWidth = cast<VectorType>(SVI.getType())->getNumElements();
868 
869  APInt UndefElts(VWidth, 0);
870  APInt AllOnesEltMask(APInt::getAllOnesValue(VWidth));
871  if (Value *V = SimplifyDemandedVectorElts(&SVI, AllOnesEltMask, UndefElts)) {
872  if (V != &SVI)
873  return ReplaceInstUsesWith(SVI, V);
874  LHS = SVI.getOperand(0);
875  RHS = SVI.getOperand(1);
876  MadeChange = true;
877  }
878 
879  unsigned LHSWidth = cast<VectorType>(LHS->getType())->getNumElements();
880 
881  // Canonicalize shuffle(x ,x,mask) -> shuffle(x, undef,mask')
882  // Canonicalize shuffle(undef,x,mask) -> shuffle(x, undef,mask').
883  if (LHS == RHS || isa<UndefValue>(LHS)) {
884  if (isa<UndefValue>(LHS) && LHS == RHS) {
885  // shuffle(undef,undef,mask) -> undef.
886  Value *Result = (VWidth == LHSWidth)
887  ? LHS : UndefValue::get(SVI.getType());
888  return ReplaceInstUsesWith(SVI, Result);
889  }
890 
891  // Remap any references to RHS to use LHS.
893  for (unsigned i = 0, e = LHSWidth; i != VWidth; ++i) {
894  if (Mask[i] < 0) {
896  continue;
897  }
898 
899  if ((Mask[i] >= (int)e && isa<UndefValue>(RHS)) ||
900  (Mask[i] < (int)e && isa<UndefValue>(LHS))) {
901  Mask[i] = -1; // Turn into undef.
903  } else {
904  Mask[i] = Mask[i] % e; // Force to LHS.
906  Mask[i]));
907  }
908  }
909  SVI.setOperand(0, SVI.getOperand(1));
910  SVI.setOperand(1, UndefValue::get(RHS->getType()));
911  SVI.setOperand(2, ConstantVector::get(Elts));
912  LHS = SVI.getOperand(0);
913  RHS = SVI.getOperand(1);
914  MadeChange = true;
915  }
916 
917  if (VWidth == LHSWidth) {
918  // Analyze the shuffle, are the LHS or RHS and identity shuffles?
919  bool isLHSID, isRHSID;
920  RecognizeIdentityMask(Mask, isLHSID, isRHSID);
921 
922  // Eliminate identity shuffles.
923  if (isLHSID) return ReplaceInstUsesWith(SVI, LHS);
924  if (isRHSID) return ReplaceInstUsesWith(SVI, RHS);
925  }
926 
927  if (isa<UndefValue>(RHS) && CanEvaluateShuffled(LHS, Mask)) {
928  Value *V = EvaluateInDifferentElementOrder(LHS, Mask);
929  return ReplaceInstUsesWith(SVI, V);
930  }
931 
932  // If the LHS is a shufflevector itself, see if we can combine it with this
933  // one without producing an unusual shuffle.
934  // Cases that might be simplified:
935  // 1.
936  // x1=shuffle(v1,v2,mask1)
937  // x=shuffle(x1,undef,mask)
938  // ==>
939  // x=shuffle(v1,undef,newMask)
940  // newMask[i] = (mask[i] < x1.size()) ? mask1[mask[i]] : -1
941  // 2.
942  // x1=shuffle(v1,undef,mask1)
943  // x=shuffle(x1,x2,mask)
944  // where v1.size() == mask1.size()
945  // ==>
946  // x=shuffle(v1,x2,newMask)
947  // newMask[i] = (mask[i] < x1.size()) ? mask1[mask[i]] : mask[i]
948  // 3.
949  // x2=shuffle(v2,undef,mask2)
950  // x=shuffle(x1,x2,mask)
951  // where v2.size() == mask2.size()
952  // ==>
953  // x=shuffle(x1,v2,newMask)
954  // newMask[i] = (mask[i] < x1.size())
955  // ? mask[i] : mask2[mask[i]-x1.size()]+x1.size()
956  // 4.
957  // x1=shuffle(v1,undef,mask1)
958  // x2=shuffle(v2,undef,mask2)
959  // x=shuffle(x1,x2,mask)
960  // where v1.size() == v2.size()
961  // ==>
962  // x=shuffle(v1,v2,newMask)
963  // newMask[i] = (mask[i] < x1.size())
964  // ? mask1[mask[i]] : mask2[mask[i]-x1.size()]+v1.size()
965  //
966  // Here we are really conservative:
967  // we are absolutely afraid of producing a shuffle mask not in the input
968  // program, because the code gen may not be smart enough to turn a merged
969  // shuffle into two specific shuffles: it may produce worse code. As such,
970  // we only merge two shuffles if the result is either a splat or one of the
971  // input shuffle masks. In this case, merging the shuffles just removes
972  // one instruction, which we know is safe. This is good for things like
973  // turning: (splat(splat)) -> splat, or
974  // merge(V[0..n], V[n+1..2n]) -> V[0..2n]
975  ShuffleVectorInst* LHSShuffle = dyn_cast<ShuffleVectorInst>(LHS);
976  ShuffleVectorInst* RHSShuffle = dyn_cast<ShuffleVectorInst>(RHS);
977  if (LHSShuffle)
978  if (!isa<UndefValue>(LHSShuffle->getOperand(1)) && !isa<UndefValue>(RHS))
979  LHSShuffle = nullptr;
980  if (RHSShuffle)
981  if (!isa<UndefValue>(RHSShuffle->getOperand(1)))
982  RHSShuffle = nullptr;
983  if (!LHSShuffle && !RHSShuffle)
984  return MadeChange ? &SVI : nullptr;
985 
986  Value* LHSOp0 = nullptr;
987  Value* LHSOp1 = nullptr;
988  Value* RHSOp0 = nullptr;
989  unsigned LHSOp0Width = 0;
990  unsigned RHSOp0Width = 0;
991  if (LHSShuffle) {
992  LHSOp0 = LHSShuffle->getOperand(0);
993  LHSOp1 = LHSShuffle->getOperand(1);
994  LHSOp0Width = cast<VectorType>(LHSOp0->getType())->getNumElements();
995  }
996  if (RHSShuffle) {
997  RHSOp0 = RHSShuffle->getOperand(0);
998  RHSOp0Width = cast<VectorType>(RHSOp0->getType())->getNumElements();
999  }
1000  Value* newLHS = LHS;
1001  Value* newRHS = RHS;
1002  if (LHSShuffle) {
1003  // case 1
1004  if (isa<UndefValue>(RHS)) {
1005  newLHS = LHSOp0;
1006  newRHS = LHSOp1;
1007  }
1008  // case 2 or 4
1009  else if (LHSOp0Width == LHSWidth) {
1010  newLHS = LHSOp0;
1011  }
1012  }
1013  // case 3 or 4
1014  if (RHSShuffle && RHSOp0Width == LHSWidth) {
1015  newRHS = RHSOp0;
1016  }
1017  // case 4
1018  if (LHSOp0 == RHSOp0) {
1019  newLHS = LHSOp0;
1020  newRHS = nullptr;
1021  }
1022 
1023  if (newLHS == LHS && newRHS == RHS)
1024  return MadeChange ? &SVI : nullptr;
1025 
1026  SmallVector<int, 16> LHSMask;
1027  SmallVector<int, 16> RHSMask;
1028  if (newLHS != LHS)
1029  LHSMask = LHSShuffle->getShuffleMask();
1030  if (RHSShuffle && newRHS != RHS)
1031  RHSMask = RHSShuffle->getShuffleMask();
1032 
1033  unsigned newLHSWidth = (newLHS != LHS) ? LHSOp0Width : LHSWidth;
1034  SmallVector<int, 16> newMask;
1035  bool isSplat = true;
1036  int SplatElt = -1;
1037  // Create a new mask for the new ShuffleVectorInst so that the new
1038  // ShuffleVectorInst is equivalent to the original one.
1039  for (unsigned i = 0; i < VWidth; ++i) {
1040  int eltMask;
1041  if (Mask[i] < 0) {
1042  // This element is an undef value.
1043  eltMask = -1;
1044  } else if (Mask[i] < (int)LHSWidth) {
1045  // This element is from left hand side vector operand.
1046  //
1047  // If LHS is going to be replaced (case 1, 2, or 4), calculate the
1048  // new mask value for the element.
1049  if (newLHS != LHS) {
1050  eltMask = LHSMask[Mask[i]];
1051  // If the value selected is an undef value, explicitly specify it
1052  // with a -1 mask value.
1053  if (eltMask >= (int)LHSOp0Width && isa<UndefValue>(LHSOp1))
1054  eltMask = -1;
1055  } else
1056  eltMask = Mask[i];
1057  } else {
1058  // This element is from right hand side vector operand
1059  //
1060  // If the value selected is an undef value, explicitly specify it
1061  // with a -1 mask value. (case 1)
1062  if (isa<UndefValue>(RHS))
1063  eltMask = -1;
1064  // If RHS is going to be replaced (case 3 or 4), calculate the
1065  // new mask value for the element.
1066  else if (newRHS != RHS) {
1067  eltMask = RHSMask[Mask[i]-LHSWidth];
1068  // If the value selected is an undef value, explicitly specify it
1069  // with a -1 mask value.
1070  if (eltMask >= (int)RHSOp0Width) {
1071  assert(isa<UndefValue>(RHSShuffle->getOperand(1))
1072  && "should have been check above");
1073  eltMask = -1;
1074  }
1075  } else
1076  eltMask = Mask[i]-LHSWidth;
1077 
1078  // If LHS's width is changed, shift the mask value accordingly.
1079  // If newRHS == NULL, i.e. LHSOp0 == RHSOp0, we want to remap any
1080  // references from RHSOp0 to LHSOp0, so we don't need to shift the mask.
1081  // If newRHS == newLHS, we want to remap any references from newRHS to
1082  // newLHS so that we can properly identify splats that may occur due to
1083  // obfuscation across the two vectors.
1084  if (eltMask >= 0 && newRHS != nullptr && newLHS != newRHS)
1085  eltMask += newLHSWidth;
1086  }
1087 
1088  // Check if this could still be a splat.
1089  if (eltMask >= 0) {
1090  if (SplatElt >= 0 && SplatElt != eltMask)
1091  isSplat = false;
1092  SplatElt = eltMask;
1093  }
1094 
1095  newMask.push_back(eltMask);
1096  }
1097 
1098  // If the result mask is equal to one of the original shuffle masks,
1099  // or is a splat, do the replacement.
1100  if (isSplat || newMask == LHSMask || newMask == RHSMask || newMask == Mask) {
1103  for (unsigned i = 0, e = newMask.size(); i != e; ++i) {
1104  if (newMask[i] < 0) {
1105  Elts.push_back(UndefValue::get(Int32Ty));
1106  } else {
1107  Elts.push_back(ConstantInt::get(Int32Ty, newMask[i]));
1108  }
1109  }
1110  if (!newRHS)
1111  newRHS = UndefValue::get(newLHS->getType());
1112  return new ShuffleVectorInst(newLHS, newRHS, ConstantVector::get(Elts));
1113  }
1114 
1115  // If the result mask is an identity, replace uses of this instruction with
1116  // corresponding argument.
1117  bool isLHSID, isRHSID;
1118  RecognizeIdentityMask(newMask, isLHSID, isRHSID);
1119  if (isLHSID && VWidth == LHSOp0Width) return ReplaceInstUsesWith(SVI, newLHS);
1120  if (isRHSID && VWidth == RHSOp0Width) return ReplaceInstUsesWith(SVI, newRHS);
1121 
1122  return MadeChange ? &SVI : nullptr;
1123 }
static APInt getAllOnesValue(unsigned numBits)
Get the all-ones value.
Definition: APInt.h:448
Value * SimplifyDemandedVectorElts(Value *V, APInt DemandedElts, APInt &UndefElts, unsigned Depth=0)
static void RecognizeIdentityMask(const SmallVectorImpl< int > &Mask, bool &isLHSID, bool &isRHSID)
static Constant * get(ArrayRef< Constant * > V)
Definition: Constants.cpp:961
static bool CanEvaluateShuffled(Value *V, ArrayRef< int > Mask, unsigned Depth=5)
assert(Globals.size() > 1)
Type * Int32Ty
Instruction * ReplaceInstUsesWith(Instruction &I, Value *V)
Definition: InstCombine.h:282
Value * getOperand(unsigned i) const
Definition: User.h:90
Value * EvaluateInDifferentElementOrder(Value *V, ArrayRef< int > Mask)
static UndefValue * get(Type *T)
Definition: Constants.cpp:1372
LLVMContext & getContext() const
All values hold a context through their type.
Definition: Value.cpp:615
Type * getType() const
Definition: Value.h:215
static Constant * get(Type *Ty, uint64_t V, bool isSigned=false)
Definition: Constants.cpp:528
void setOperand(unsigned i, Value *Val)
Definition: User.h:94
Class for arbitrary precision integers.
Definition: APInt.h:75
LLVM_ATTRIBUTE_UNUSED_RESULT std::enable_if< !is_simple_type< Y >::value, typename cast_retty< X, const Y >::ret_type >::type dyn_cast(const Y &Val)
Definition: Casting.h:265
static IntegerType * getInt32Ty(LLVMContext &C)
Definition: Type.cpp:235
VectorType * getType() const
LLVM Value Representation.
Definition: Value.h:69
static void getShuffleMask(Constant *Mask, SmallVectorImpl< int > &Result)
Instruction * InstCombiner::visitSIToFP ( CastInst CI)

Definition at line 1402 of file InstCombineCasts.cpp.

1402  {
1403  return commonCastTransforms(CI);
1404 }
Instruction * commonCastTransforms(CastInst &CI)
Implement the transforms common to all CastInst visitors.
Instruction * llvm::InstVisitor< InstCombiner , Instruction * >::visitSIToFPInst ( SIToFPInst I)
inlineinherited

Definition at line 190 of file InstVisitor.h.

190 { DELEGATE(CastInst);}
#define DELEGATE(CLASS_TO_VISIT)
Definition: InstVisitor.h:30
Instruction * InstCombiner::visitSRem ( BinaryOperator I)

Definition at line 1261 of file InstCombineMulDivRem.cpp.

1261  {
1262  Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
1263 
1264  if (Value *V = SimplifyVectorOp(I))
1265  return ReplaceInstUsesWith(I, V);
1266 
1267  if (Value *V = SimplifySRemInst(Op0, Op1, DL))
1268  return ReplaceInstUsesWith(I, V);
1269 
1270  // Handle the integer rem common cases
1271  if (Instruction *Common = commonIRemTransforms(I))
1272  return Common;
1273 
1274  if (Value *RHSNeg = dyn_castNegVal(Op1))
1275  if (!isa<Constant>(RHSNeg) ||
1276  (isa<ConstantInt>(RHSNeg) &&
1277  cast<ConstantInt>(RHSNeg)->getValue().isStrictlyPositive())) {
1278  // X % -Y -> X % Y
1280  I.setOperand(1, RHSNeg);
1281  return &I;
1282  }
1283 
1284  // If the sign bits of both operands are zero (i.e. we can prove they are
1285  // unsigned inputs), turn this into a urem.
1286  if (I.getType()->isIntegerTy()) {
1288  if (MaskedValueIsZero(Op1, Mask) && MaskedValueIsZero(Op0, Mask)) {
1289  // X srem Y -> X urem Y, iff X and Y don't have sign bit set
1290  return BinaryOperator::CreateURem(Op0, Op1, I.getName());
1291  }
1292  }
1293 
1294  // If it's a constant vector, flip any negative values positive.
1295  if (isa<ConstantVector>(Op1) || isa<ConstantDataVector>(Op1)) {
1296  Constant *C = cast<Constant>(Op1);
1297  unsigned VWidth = C->getType()->getVectorNumElements();
1298 
1299  bool hasNegative = false;
1300  bool hasMissing = false;
1301  for (unsigned i = 0; i != VWidth; ++i) {
1302  Constant *Elt = C->getAggregateElement(i);
1303  if (!Elt) {
1304  hasMissing = true;
1305  break;
1306  }
1307 
1308  if (ConstantInt *RHS = dyn_cast<ConstantInt>(Elt))
1309  if (RHS->isNegative())
1310  hasNegative = true;
1311  }
1312 
1313  if (hasNegative && !hasMissing) {
1314  SmallVector<Constant *, 16> Elts(VWidth);
1315  for (unsigned i = 0; i != VWidth; ++i) {
1316  Elts[i] = C->getAggregateElement(i); // Handle undef, etc.
1317  if (ConstantInt *RHS = dyn_cast<ConstantInt>(Elts[i])) {
1318  if (RHS->isNegative())
1319  Elts[i] = cast<ConstantInt>(ConstantExpr::getNeg(RHS));
1320  }
1321  }
1322 
1323  Constant *NewRHSV = ConstantVector::get(Elts);
1324  if (NewRHSV != C) { // Don't loop on -MININT
1326  I.setOperand(1, NewRHSV);
1327  return &I;
1328  }
1329  }
1330  }
1331 
1332  return nullptr;
1333 }
static APInt getSignBit(unsigned BitWidth)
Get the SignBit for a specific bit width.
Definition: APInt.h:441
Value * SimplifyVectorOp(BinaryOperator &Inst)
Makes transformation of binary operation specific for vector types.
StringRef getName() const
Definition: Value.cpp:168
Instruction * commonIRemTransforms(BinaryOperator &I)
Common integer remainder transforms.
static Constant * get(ArrayRef< Constant * > V)
Definition: Constants.cpp:961
Value * dyn_castNegVal(Value *V) const
LLVM Constant Representation.
Definition: Constant.h:41
Instruction * ReplaceInstUsesWith(Instruction &I, Value *V)
Definition: InstCombine.h:282
Value * getOperand(unsigned i) const
Definition: User.h:90
InstCombineWorklist Worklist
Worklist - All of the instructions that need to be simplified.
Definition: InstCombine.h:97
Constant * getAggregateElement(unsigned Elt) const
Definition: Constants.cpp:205
Class for constant integers.
Definition: Constants.h:51
unsigned getVectorNumElements() const
Definition: Type.cpp:208
Value * SimplifySRemInst(Value *LHS, Value *RHS, const DataLayout *TD=nullptr, const TargetLibraryInfo *TLI=nullptr, const DominatorTree *DT=nullptr)
Type * getType() const
Definition: Value.h:215
void setOperand(unsigned i, Value *Val)
Definition: User.h:94
Class for arbitrary precision integers.
Definition: APInt.h:75
bool isIntegerTy() const
Definition: Type.h:193
static Constant * getNeg(Constant *C, bool HasNUW=false, bool HasNSW=false)
Definition: Constants.cpp:2083
#define I(x, y, z)
Definition: MD5.cpp:54
bool MaskedValueIsZero(Value *V, const APInt &Mask, unsigned Depth=0) const
Definition: InstCombine.h:323
const DataLayout * DL
Definition: InstCombine.h:89
unsigned getPrimitiveSizeInBits() const LLVM_READONLY
Definition: Type.cpp:117
LLVM Value Representation.
Definition: Value.h:69
Instruction * InstCombiner::visitStoreInst ( StoreInst SI)

Definition at line 596 of file InstCombineLoadStoreAlloca.cpp.

596  {
597  Value *Val = SI.getOperand(0);
598  Value *Ptr = SI.getOperand(1);
599 
600  // Attempt to improve the alignment.
601  if (DL) {
602  unsigned KnownAlign =
604  DL);
605  unsigned StoreAlign = SI.getAlignment();
606  unsigned EffectiveStoreAlign = StoreAlign != 0 ? StoreAlign :
607  DL->getABITypeAlignment(Val->getType());
608 
609  if (KnownAlign > EffectiveStoreAlign)
610  SI.setAlignment(KnownAlign);
611  else if (StoreAlign == 0)
612  SI.setAlignment(EffectiveStoreAlign);
613  }
614 
615  // Don't hack volatile/atomic stores.
616  // FIXME: Some bits are legal for atomic stores; needs refactoring.
617  if (!SI.isSimple()) return nullptr;
618 
619  // If the RHS is an alloca with a single use, zapify the store, making the
620  // alloca dead.
621  if (Ptr->hasOneUse()) {
622  if (isa<AllocaInst>(Ptr))
623  return EraseInstFromFunction(SI);
624  if (GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(Ptr)) {
625  if (isa<AllocaInst>(GEP->getOperand(0))) {
626  if (GEP->getOperand(0)->hasOneUse())
627  return EraseInstFromFunction(SI);
628  }
629  }
630  }
631 
632  // Do really simple DSE, to catch cases where there are several consecutive
633  // stores to the same location, separated by a few arithmetic operations. This
634  // situation often occurs with bitfield accesses.
635  BasicBlock::iterator BBI = &SI;
636  for (unsigned ScanInsts = 6; BBI != SI.getParent()->begin() && ScanInsts;
637  --ScanInsts) {
638  --BBI;
639  // Don't count debug info directives, lest they affect codegen,
640  // and we skip pointer-to-pointer bitcasts, which are NOPs.
641  if (isa<DbgInfoIntrinsic>(BBI) ||
642  (isa<BitCastInst>(BBI) && BBI->getType()->isPointerTy())) {
643  ScanInsts++;
644  continue;
645  }
646 
647  if (StoreInst *PrevSI = dyn_cast<StoreInst>(BBI)) {
648  // Prev store isn't volatile, and stores to the same location?
649  if (PrevSI->isSimple() && equivalentAddressValues(PrevSI->getOperand(1),
650  SI.getOperand(1))) {
651  ++NumDeadStore;
652  ++BBI;
653  EraseInstFromFunction(*PrevSI);
654  continue;
655  }
656  break;
657  }
658 
659  // If this is a load, we have to stop. However, if the loaded value is from
660  // the pointer we're loading and is producing the pointer we're storing,
661  // then *this* store is dead (X = load P; store X -> P).
662  if (LoadInst *LI = dyn_cast<LoadInst>(BBI)) {
663  if (LI == Val && equivalentAddressValues(LI->getOperand(0), Ptr) &&
664  LI->isSimple())
665  return EraseInstFromFunction(SI);
666 
667  // Otherwise, this is a load from some other location. Stores before it
668  // may not be dead.
669  break;
670  }
671 
672  // Don't skip over loads or things that can modify memory.
673  if (BBI->mayWriteToMemory() || BBI->mayReadFromMemory())
674  break;
675  }
676 
677  // store X, null -> turns into 'unreachable' in SimplifyCFG
678  if (isa<ConstantPointerNull>(Ptr) && SI.getPointerAddressSpace() == 0) {
679  if (!isa<UndefValue>(Val)) {
680  SI.setOperand(0, UndefValue::get(Val->getType()));
681  if (Instruction *U = dyn_cast<Instruction>(Val))
682  Worklist.Add(U); // Dropped a use.
683  }
684  return nullptr; // Do not modify these!
685  }
686 
687  // store undef, Ptr -> noop
688  if (isa<UndefValue>(Val))
689  return EraseInstFromFunction(SI);
690 
691  // If the pointer destination is a cast, see if we can fold the cast into the
692  // source instead.
693  if (isa<CastInst>(Ptr))
694  if (Instruction *Res = InstCombineStoreToCast(*this, SI))
695  return Res;
696  if (ConstantExpr *CE = dyn_cast<ConstantExpr>(Ptr))
697  if (CE->isCast())
698  if (Instruction *Res = InstCombineStoreToCast(*this, SI))
699  return Res;
700 
701 
702  // If this store is the last instruction in the basic block (possibly
703  // excepting debug info instructions), and if the block ends with an
704  // unconditional branch, try to move it to the successor block.
705  BBI = &SI;
706  do {
707  ++BBI;
708  } while (isa<DbgInfoIntrinsic>(BBI) ||
709  (isa<BitCastInst>(BBI) && BBI->getType()->isPointerTy()));
710  if (BranchInst *BI = dyn_cast<BranchInst>(BBI))
711  if (BI->isUnconditional())
713  return nullptr; // xform done!
714 
715  return nullptr;
716 }
unsigned getOrEnforceKnownAlignment(Value *V, unsigned PrefAlign, const DataLayout *TD=nullptr)
unsigned getPrefTypeAlignment(Type *Ty) const
Definition: DataLayout.cpp:693
bool isSimple() const
Definition: Instructions.h:355
void Add(Instruction *I)
LoopInfoBase< BlockT, LoopT > * LI
Definition: LoopInfoImpl.h:412
iterator begin()
Definition: BasicBlock.h:198
unsigned getPointerAddressSpace() const
Returns the address space of the pointer operand.
Definition: Instructions.h:368
static Instruction * InstCombineStoreToCast(InstCombiner &IC, StoreInst &SI)
static bool equivalentAddressValues(Value *A, Value *B)
unsigned getAlignment() const
Definition: Instructions.h:318
Value * getOperand(unsigned i) const
Definition: User.h:90
InstCombineWorklist Worklist
Worklist - All of the instructions that need to be simplified.
Definition: InstCombine.h:97
static UndefValue * get(Type *T)
Definition: Constants.cpp:1372
unsigned getABITypeAlignment(Type *Ty) const
Definition: DataLayout.cpp:683
Type * getType() const
Definition: Value.h:215
void setOperand(unsigned i, Value *Val)
Definition: User.h:94
bool SimplifyStoreAtEndOfBlock(StoreInst &SI)
bool hasOneUse() const
Definition: Value.h:284
const DataLayout * DL
Definition: InstCombine.h:89
LLVM Value Representation.
Definition: Value.h:69
void setAlignment(unsigned Align)
Instruction * EraseInstFromFunction(Instruction &I)
Definition: InstCombine.h:301
const BasicBlock * getParent() const
Definition: Instruction.h:53
Instruction * InstCombiner::visitSub ( BinaryOperator I)

Definition at line 1451 of file InstCombineAddSub.cpp.

1451  {
1452  Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
1453 
1454  if (Value *V = SimplifyVectorOp(I))
1455  return ReplaceInstUsesWith(I, V);
1456 
1457  if (Value *V = SimplifySubInst(Op0, Op1, I.hasNoSignedWrap(),
1458  I.hasNoUnsignedWrap(), DL))
1459  return ReplaceInstUsesWith(I, V);
1460 
1461  // (A*B)-(A*C) -> A*(B-C) etc
1463  return ReplaceInstUsesWith(I, V);
1464 
1465  // If this is a 'B = x-(-A)', change to B = x+A. This preserves NSW/NUW.
1466  if (Value *V = dyn_castNegVal(Op1)) {
1467  BinaryOperator *Res = BinaryOperator::CreateAdd(Op0, V);
1470  return Res;
1471  }
1472 
1473  if (I.getType()->isIntegerTy(1))
1474  return BinaryOperator::CreateXor(Op0, Op1);
1475 
1476  // Replace (-1 - A) with (~A).
1477  if (match(Op0, m_AllOnes()))
1478  return BinaryOperator::CreateNot(Op1);
1479 
1480  if (Constant *C = dyn_cast<Constant>(Op0)) {
1481  // C - ~X == X + (1+C)
1482  Value *X = nullptr;
1483  if (match(Op1, m_Not(m_Value(X))))
1484  return BinaryOperator::CreateAdd(X, AddOne(C));
1485 
1486  // Try to fold constant sub into select arguments.
1487  if (SelectInst *SI = dyn_cast<SelectInst>(Op1))
1488  if (Instruction *R = FoldOpIntoSelect(I, SI))
1489  return R;
1490 
1491  // C-(X+C2) --> (C-C2)-X
1492  Constant *C2;
1493  if (match(Op1, m_Add(m_Value(X), m_Constant(C2))))
1494  return BinaryOperator::CreateSub(ConstantExpr::getSub(C, C2), X);
1495 
1497  return &I;
1498 
1499  // Fold (sub 0, (zext bool to B)) --> (sext bool to B)
1500  if (C->isNullValue() && match(Op1, m_ZExt(m_Value(X))))
1501  if (X->getType()->getScalarType()->isIntegerTy(1))
1502  return CastInst::CreateSExtOrBitCast(X, Op1->getType());
1503 
1504  // Fold (sub 0, (sext bool to B)) --> (zext bool to B)
1505  if (C->isNullValue() && match(Op1, m_SExt(m_Value(X))))
1506  if (X->getType()->getScalarType()->isIntegerTy(1))
1507  return CastInst::CreateZExtOrBitCast(X, Op1->getType());
1508  }
1509 
1510  if (ConstantInt *C = dyn_cast<ConstantInt>(Op0)) {
1511  // -(X >>u 31) -> (X >>s 31)
1512  // -(X >>s 31) -> (X >>u 31)
1513  if (C->isZero()) {
1514  Value *X; ConstantInt *CI;
1515  if (match(Op1, m_LShr(m_Value(X), m_ConstantInt(CI))) &&
1516  // Verify we are shifting out everything but the sign bit.
1517  CI->getValue() == I.getType()->getPrimitiveSizeInBits()-1)
1518  return BinaryOperator::CreateAShr(X, CI);
1519 
1520  if (match(Op1, m_AShr(m_Value(X), m_ConstantInt(CI))) &&
1521  // Verify we are shifting out everything but the sign bit.
1522  CI->getValue() == I.getType()->getPrimitiveSizeInBits()-1)
1523  return BinaryOperator::CreateLShr(X, CI);
1524  }
1525  }
1526 
1527 
1528  { Value *Y;
1529  // X-(X+Y) == -Y X-(Y+X) == -Y
1530  if (match(Op1, m_Add(m_Specific(Op0), m_Value(Y))) ||
1531  match(Op1, m_Add(m_Value(Y), m_Specific(Op0))))
1532  return BinaryOperator::CreateNeg(Y);
1533 
1534  // (X-Y)-X == -Y
1535  if (match(Op0, m_Sub(m_Specific(Op1), m_Value(Y))))
1536  return BinaryOperator::CreateNeg(Y);
1537  }
1538 
1539  if (Op1->hasOneUse()) {
1540  Value *X = nullptr, *Y = nullptr, *Z = nullptr;
1541  Constant *C = nullptr;
1542  Constant *CI = nullptr;
1543 
1544  // (X - (Y - Z)) --> (X + (Z - Y)).
1545  if (match(Op1, m_Sub(m_Value(Y), m_Value(Z))))
1546  return BinaryOperator::CreateAdd(Op0,
1547  Builder->CreateSub(Z, Y, Op1->getName()));
1548 
1549  // (X - (X & Y)) --> (X & ~Y)
1550  //
1551  if (match(Op1, m_And(m_Value(Y), m_Specific(Op0))) ||
1552  match(Op1, m_And(m_Specific(Op0), m_Value(Y))))
1553  return BinaryOperator::CreateAnd(Op0,
1554  Builder->CreateNot(Y, Y->getName() + ".not"));
1555 
1556  // 0 - (X sdiv C) -> (X sdiv -C) provided the negation doesn't overflow.
1557  if (match(Op1, m_SDiv(m_Value(X), m_Constant(C))) && match(Op0, m_Zero()) &&
1558  !C->isMinSignedValue())
1559  return BinaryOperator::CreateSDiv(X, ConstantExpr::getNeg(C));
1560 
1561  // 0 - (X << Y) -> (-X << Y) when X is freely negatable.
1562  if (match(Op1, m_Shl(m_Value(X), m_Value(Y))) && match(Op0, m_Zero()))
1563  if (Value *XNeg = dyn_castNegVal(X))
1564  return BinaryOperator::CreateShl(XNeg, Y);
1565 
1566  // X - A*-B -> X + A*B
1567  // X - -A*B -> X + A*B
1568  Value *A, *B;
1569  if (match(Op1, m_Mul(m_Value(A), m_Neg(m_Value(B)))) ||
1570  match(Op1, m_Mul(m_Neg(m_Value(A)), m_Value(B))))
1571  return BinaryOperator::CreateAdd(Op0, Builder->CreateMul(A, B));
1572 
1573  // X - A*CI -> X + A*-CI
1574  // X - CI*A -> X + A*-CI
1575  if (match(Op1, m_Mul(m_Value(A), m_Constant(CI))) ||
1576  match(Op1, m_Mul(m_Constant(CI), m_Value(A)))) {
1577  Value *NewMul = Builder->CreateMul(A, ConstantExpr::getNeg(CI));
1578  return BinaryOperator::CreateAdd(Op0, NewMul);
1579  }
1580  }
1581 
1582  // Optimize pointer differences into the same array into a size. Consider:
1583  // &A[10] - &A[0]: we should compile this to "10".
1584  if (DL) {
1585  Value *LHSOp, *RHSOp;
1586  if (match(Op0, m_PtrToInt(m_Value(LHSOp))) &&
1587  match(Op1, m_PtrToInt(m_Value(RHSOp))))
1588  if (Value *Res = OptimizePointerDifference(LHSOp, RHSOp, I.getType()))
1589  return ReplaceInstUsesWith(I, Res);
1590 
1591  // trunc(p)-trunc(q) -> trunc(p-q)
1592  if (match(Op0, m_Trunc(m_PtrToInt(m_Value(LHSOp)))) &&
1593  match(Op1, m_Trunc(m_PtrToInt(m_Value(RHSOp)))))
1594  if (Value *Res = OptimizePointerDifference(LHSOp, RHSOp, I.getType()))
1595  return ReplaceInstUsesWith(I, Res);
1596  }
1597 
1598  return nullptr;
1599 }
BinaryOp_match< LHS, RHS, Instruction::And > m_And(const LHS &L, const RHS &R)
Definition: PatternMatch.h:467
class_match< Value > m_Value()
m_Value() - Match an arbitrary value and ignore it.
Definition: PatternMatch.h:70
void setHasNoSignedWrap(bool b=true)
BinaryOp_match< LHS, RHS, Instruction::Sub > m_Sub(const LHS &L, const RHS &R)
Definition: PatternMatch.h:407
static BinaryOperator * CreateNot(Value *Op, const Twine &Name="", Instruction *InsertBefore=nullptr)
match_zero m_Zero()
Definition: PatternMatch.h:137
BinaryOp_match< LHS, RHS, Instruction::Mul > m_Mul(const LHS &L, const RHS &R)
Definition: PatternMatch.h:419
Value * SimplifyVectorOp(BinaryOperator &Inst)
Makes transformation of binary operation specific for vector types.
class_match< Constant > m_Constant()
Definition: PatternMatch.h:78
BinaryOp_match< LHS, RHS, Instruction::AShr > m_AShr(const LHS &L, const RHS &R)
Definition: PatternMatch.h:497
static RegisterPass< NVPTXAllocaHoisting > X("alloca-hoisting","Hoisting alloca instructions in non-entry ""blocks to the entry block")
Instruction * FoldOpIntoSelect(Instruction &Op, SelectInst *SI)
static Constant * getSub(Constant *C1, Constant *C2, bool HasNUW=false, bool HasNSW=false)
Definition: Constants.cpp:2113
Value * CreateSub(Value *LHS, Value *RHS, const Twine &Name="", bool HasNUW=false, bool HasNSW=false)
Definition: IRBuilder.h:654
StringRef getName() const
Definition: Value.cpp:168
bool match(Val *V, const Pattern &P)
Definition: PatternMatch.h:42
const APInt & getValue() const
Return the constant's value.
Definition: Constants.h:105
void setHasNoUnsignedWrap(bool b=true)
CastClass_match< OpTy, Instruction::Trunc > m_Trunc(const OpTy &Op)
m_Trunc
Definition: PatternMatch.h:768
static Constant * AddOne(Constant *C)
AddOne - Add one to a Constant.
Definition: InstCombine.h:60
not_match< LHS > m_Not(const LHS &L)
Definition: PatternMatch.h:828
BinaryOp_match< LHS, RHS, Instruction::Add > m_Add(const LHS &L, const RHS &R)
Definition: PatternMatch.h:395
Value * SimplifyUsingDistributiveLaws(BinaryOperator &I)
CastClass_match< OpTy, Instruction::ZExt > m_ZExt(const OpTy &Op)
m_ZExt
Definition: PatternMatch.h:782
class_match< ConstantInt > m_ConstantInt()
m_ConstantInt() - Match an arbitrary ConstantInt and ignore it.
Definition: PatternMatch.h:72
Value * dyn_castNegVal(Value *V) const
Value * OptimizePointerDifference(Value *LHS, Value *RHS, Type *Ty)
BinaryOp_match< LHS, RHS, Instruction::LShr > m_LShr(const LHS &L, const RHS &R)
Definition: PatternMatch.h:491
BinaryOp_match< LHS, RHS, Instruction::SDiv > m_SDiv(const LHS &L, const RHS &R)
Definition: PatternMatch.h:437
Value * CreateNot(Value *V, const Twine &Name="")
Definition: IRBuilder.h:885
LLVM Constant Representation.
Definition: Constant.h:41
Instruction * ReplaceInstUsesWith(Instruction &I, Value *V)
Definition: InstCombine.h:282
cst_pred_ty< is_all_ones > m_AllOnes()
m_AllOnes() - Match an integer or vector with all bits set to true.
Definition: PatternMatch.h:265
specificval_ty m_Specific(const Value *V)
m_Specific - Match if we have a specific specified value.
Definition: PatternMatch.h:323
BinaryOp_match< LHS, RHS, Instruction::Shl > m_Shl(const LHS &L, const RHS &R)
Definition: PatternMatch.h:485
Value * getOperand(unsigned i) const
Definition: User.h:90
BuilderTy * Builder
Definition: InstCombine.h:102
NUW NUW NUW NUW Exact static Exact BinaryOperator * CreateNeg(Value *Op, const Twine &Name="", Instruction *InsertBefore=nullptr)
static CastInst * CreateZExtOrBitCast(Value *S, Type *Ty, const Twine &Name="", Instruction *InsertBefore=nullptr)
Create a ZExt or BitCast cast instruction.
bool hasNoSignedWrap() const
hasNoSignedWrap - Determine whether the no signed wrap flag is set.
neg_match< LHS > m_Neg(const LHS &L)
m_Neg - Match an integer negate.
Definition: PatternMatch.h:854
CastClass_match< OpTy, Instruction::SExt > m_SExt(const OpTy &Op)
m_SExt
Definition: PatternMatch.h:775
Class for constant integers.
Definition: Constants.h:51
Type * getType() const
Definition: Value.h:215
Value * CreateMul(Value *LHS, Value *RHS, const Twine &Name="", bool HasNUW=false, bool HasNSW=false)
Definition: IRBuilder.h:676
static RegisterPass< SwOnly > Z("legup-sw-only","Replace accelerated functions with wrappers and produce legup_wrappers")
bool isIntegerTy() const
Definition: Type.h:193
CastClass_match< OpTy, Instruction::PtrToInt > m_PtrToInt(const OpTy &Op)
m_PtrToInt
Definition: PatternMatch.h:761
const Type * getScalarType() const LLVM_READONLY
Definition: Type.cpp:51
static Constant * getNeg(Constant *C, bool HasNUW=false, bool HasNSW=false)
Definition: Constants.cpp:2083
Value * SimplifySubInst(Value *LHS, Value *RHS, bool isNSW, bool isNUW, const DataLayout *TD=nullptr, const TargetLibraryInfo *TLI=nullptr, const DominatorTree *DT=nullptr)
static CastInst * CreateSExtOrBitCast(Value *S, Type *Ty, const Twine &Name="", Instruction *InsertBefore=nullptr)
Create a SExt or BitCast cast instruction.
const DataLayout * DL
Definition: InstCombine.h:89
unsigned getPrimitiveSizeInBits() const LLVM_READONLY
Definition: Type.cpp:117
LLVM Value Representation.
Definition: Value.h:69
bool hasNoUnsignedWrap() const
hasNoUnsignedWrap - Determine whether the no unsigned wrap flag is set.
static GCMetadataPrinterRegistry::Add< OcamlGCMetadataPrinter > Y("ocaml","ocaml 3.10-compatible collector")
bool SimplifyDemandedInstructionBits(Instruction &Inst)
bool isMinSignedValue() const
Return true if the value is the smallest signed value.
Definition: Constants.cpp:110
Instruction * InstCombiner::visitSwitchInst ( SwitchInst SI)

Definition at line 1975 of file InstructionCombining.cpp.

1975  {
1976  Value *Cond = SI.getCondition();
1977  if (Instruction *I = dyn_cast<Instruction>(Cond)) {
1978  if (I->getOpcode() == Instruction::Add)
1979  if (ConstantInt *AddRHS = dyn_cast<ConstantInt>(I->getOperand(1))) {
1980  // change 'switch (X+4) case 1:' into 'switch (X) case -3'
1981  // Skip the first item since that's the default case.
1982  for (SwitchInst::CaseIt i = SI.case_begin(), e = SI.case_end();
1983  i != e; ++i) {
1984  ConstantInt* CaseVal = i.getCaseValue();
1985  Constant* NewCaseVal = ConstantExpr::getSub(cast<Constant>(CaseVal),
1986  AddRHS);
1987  assert(isa<ConstantInt>(NewCaseVal) &&
1988  "Result of expression should be constant");
1989  i.setValue(cast<ConstantInt>(NewCaseVal));
1990  }
1991  SI.setCondition(I->getOperand(0));
1992  Worklist.Add(I);
1993  return &SI;
1994  }
1995  }
1996  return nullptr;
1997 }
void Add(Instruction *I)
static Constant * getSub(Constant *C1, Constant *C2, bool HasNUW=false, bool HasNSW=false)
Definition: Constants.cpp:2113
assert(Globals.size() > 1)
LLVM Constant Representation.
Definition: Constant.h:41
InstCombineWorklist Worklist
Worklist - All of the instructions that need to be simplified.
Definition: InstCombine.h:97
Class for constant integers.
Definition: Constants.h:51
Value * getCondition() const
void setCondition(Value *V)
#define I(x, y, z)
Definition: MD5.cpp:54
LLVM Value Representation.
Definition: Value.h:69
Instruction * llvm::InstVisitor< InstCombiner , Instruction * >::visitTerminatorInst ( TerminatorInst I)
inlineinherited

Definition at line 234 of file InstVisitor.h.

#define DELEGATE(CLASS_TO_VISIT)
Definition: InstVisitor.h:30
Instruction * InstCombiner::visitTrunc ( TruncInst CI)

Definition at line 439 of file InstCombineCasts.cpp.

439  {
440  if (Instruction *Result = commonCastTransforms(CI))
441  return Result;
442 
443  // See if we can simplify any instructions used by the input whose sole
444  // purpose is to compute bits we don't care about.
446  return &CI;
447 
448  Value *Src = CI.getOperand(0);
449  Type *DestTy = CI.getType(), *SrcTy = Src->getType();
450 
451  // Attempt to truncate the entire input expression tree to the destination
452  // type. Only do this if the dest type is a simple type, don't convert the
453  // expression tree to something weird like i93 unless the source is also
454  // strange.
455  if ((DestTy->isVectorTy() || ShouldChangeType(SrcTy, DestTy)) &&
456  CanEvaluateTruncated(Src, DestTy)) {
457 
458  // If this cast is a truncate, evaluting in a different type always
459  // eliminates the cast, so it is always a win.
460  DEBUG(dbgs() << "ICE: EvaluateInDifferentType converting expression type"
461  " to avoid cast: " << CI << '\n');
462  Value *Res = EvaluateInDifferentType(Src, DestTy, false);
463  assert(Res->getType() == DestTy);
464  return ReplaceInstUsesWith(CI, Res);
465  }
466 
467  // Canonicalize trunc x to i1 -> (icmp ne (and x, 1), 0), likewise for vector.
468  if (DestTy->getScalarSizeInBits() == 1) {
469  Constant *One = ConstantInt::get(Src->getType(), 1);
470  Src = Builder->CreateAnd(Src, One);
471  Value *Zero = Constant::getNullValue(Src->getType());
472  return new ICmpInst(ICmpInst::ICMP_NE, Src, Zero);
473  }
474 
475  // Transform trunc(lshr (zext A), Cst) to eliminate one type conversion.
476  Value *A = nullptr; ConstantInt *Cst = nullptr;
477  if (Src->hasOneUse() &&
478  match(Src, m_LShr(m_ZExt(m_Value(A)), m_ConstantInt(Cst)))) {
479  // We have three types to worry about here, the type of A, the source of
480  // the truncate (MidSize), and the destination of the truncate. We know that
481  // ASize < MidSize and MidSize > ResultSize, but don't know the relation
482  // between ASize and ResultSize.
483  unsigned ASize = A->getType()->getPrimitiveSizeInBits();
484 
485  // If the shift amount is larger than the size of A, then the result is
486  // known to be zero because all the input bits got shifted out.
487  if (Cst->getZExtValue() >= ASize)
489 
490  // Since we're doing an lshr and a zero extend, and know that the shift
491  // amount is smaller than ASize, it is always safe to do the shift in A's
492  // type, then zero extend or truncate to the result.
493  Value *Shift = Builder->CreateLShr(A, Cst->getZExtValue());
494  Shift->takeName(Src);
495  return CastInst::CreateIntegerCast(Shift, CI.getType(), false);
496  }
497 
498  // Transform "trunc (and X, cst)" -> "and (trunc X), cst" so long as the dest
499  // type isn't non-native.
500  if (Src->hasOneUse() && isa<IntegerType>(Src->getType()) &&
501  ShouldChangeType(Src->getType(), CI.getType()) &&
502  match(Src, m_And(m_Value(A), m_ConstantInt(Cst)))) {
503  Value *NewTrunc = Builder->CreateTrunc(A, CI.getType(), A->getName()+".tr");
504  return BinaryOperator::CreateAnd(NewTrunc,
505  ConstantExpr::getTrunc(Cst, CI.getType()));
506  }
507 
508  return nullptr;
509 }
Value * CreateLShr(Value *LHS, Value *RHS, const Twine &Name="", bool isExact=false)
Definition: IRBuilder.h:770
BinaryOp_match< LHS, RHS, Instruction::And > m_And(const LHS &L, const RHS &R)
Definition: PatternMatch.h:467
class_match< Value > m_Value()
m_Value() - Match an arbitrary value and ignore it.
Definition: PatternMatch.h:70
Instruction * commonCastTransforms(CastInst &CI)
Implement the transforms common to all CastInst visitors.
static Constant * getNullValue(Type *Ty)
Definition: Constants.cpp:133
StringRef getName() const
Definition: Value.cpp:168
bool match(Val *V, const Pattern &P)
Definition: PatternMatch.h:42
uint64_t getZExtValue() const
Return the zero extended value.
Definition: Constants.h:116
Value * CreateAnd(Value *LHS, Value *RHS, const Twine &Name="")
Definition: IRBuilder.h:806
static bool CanEvaluateTruncated(Value *V, Type *Ty)
CastClass_match< OpTy, Instruction::ZExt > m_ZExt(const OpTy &Op)
m_ZExt
Definition: PatternMatch.h:782
class_match< ConstantInt > m_ConstantInt()
m_ConstantInt() - Match an arbitrary ConstantInt and ignore it.
Definition: PatternMatch.h:72
void takeName(Value *V)
Definition: Value.cpp:242
Value * EvaluateInDifferentType(Value *V, Type *Ty, bool isSigned)
BinaryOp_match< LHS, RHS, Instruction::LShr > m_LShr(const LHS &L, const RHS &R)
Definition: PatternMatch.h:491
assert(Globals.size() > 1)
bool isVectorTy() const
Definition: Type.h:226
LLVM Constant Representation.
Definition: Constant.h:41
Instruction * ReplaceInstUsesWith(Instruction &I, Value *V)
Definition: InstCombine.h:282
Represent an integer comparison operator.
Definition: Instructions.h:977
Value * getOperand(unsigned i) const
Definition: User.h:90
#define DEBUG(X)
BuilderTy * Builder
Definition: InstCombine.h:102
Definition: test.h:1
static CastInst * CreateIntegerCast(Value *S, Type *Ty, bool isSigned, const Twine &Name="", Instruction *InsertBefore=nullptr)
Create a ZExt, BitCast, or Trunc for int -> int casts.
Class for constant integers.
Definition: Constants.h:51
unsigned getScalarSizeInBits() const LLVM_READONLY
Definition: Type.cpp:135
Type * getType() const
Definition: Value.h:215
static Constant * get(Type *Ty, uint64_t V, bool isSigned=false)
Definition: Constants.cpp:528
static Constant * getTrunc(Constant *C, Type *Ty)
Definition: Constants.cpp:1587
raw_ostream & dbgs()
dbgs - Return a circular-buffered debug stream.
bool ShouldChangeType(Type *From, Type *To) const
unsigned getPrimitiveSizeInBits() const LLVM_READONLY
Definition: Type.cpp:117
Value * CreateTrunc(Value *V, Type *DestTy, const Twine &Name="")
Definition: IRBuilder.h:1094
LLVM Value Representation.
Definition: Value.h:69
bool SimplifyDemandedInstructionBits(Instruction &Inst)
Instruction * llvm::InstVisitor< InstCombiner , Instruction * >::visitTruncInst ( TruncInst I)
inlineinherited

Definition at line 182 of file InstVisitor.h.

182 { DELEGATE(CastInst);}
#define DELEGATE(CLASS_TO_VISIT)
Definition: InstVisitor.h:30
Instruction * InstCombiner::visitUDiv ( BinaryOperator I)

Definition at line 906 of file InstCombineMulDivRem.cpp.

906  {
907  Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
908 
909  if (Value *V = SimplifyVectorOp(I))
910  return ReplaceInstUsesWith(I, V);
911 
912  if (Value *V = SimplifyUDivInst(Op0, Op1, DL))
913  return ReplaceInstUsesWith(I, V);
914 
915  // Handle the integer div common cases
916  if (Instruction *Common = commonIDivTransforms(I))
917  return Common;
918 
919  // (x lshr C1) udiv C2 --> x udiv (C2 << C1)
920  if (Constant *C2 = dyn_cast<Constant>(Op1)) {
921  Value *X;
922  Constant *C1;
923  if (match(Op0, m_LShr(m_Value(X), m_Constant(C1))))
924  return BinaryOperator::CreateUDiv(X, ConstantExpr::getShl(C2, C1));
925  }
926 
927  // (zext A) udiv (zext B) --> zext (A udiv B)
928  if (ZExtInst *ZOp0 = dyn_cast<ZExtInst>(Op0))
929  if (Value *ZOp1 = dyn_castZExtVal(Op1, ZOp0->getSrcTy()))
930  return new ZExtInst(Builder->CreateUDiv(ZOp0->getOperand(0), ZOp1, "div",
931  I.isExact()),
932  I.getType());
933 
934  // (LHS udiv (select (select (...)))) -> (LHS >> (select (select (...))))
935  SmallVector<UDivFoldAction, 6> UDivActions;
936  if (visitUDivOperand(Op0, Op1, I, UDivActions))
937  for (unsigned i = 0, e = UDivActions.size(); i != e; ++i) {
938  FoldUDivOperandCb Action = UDivActions[i].FoldAction;
939  Value *ActionOp1 = UDivActions[i].OperandToFold;
940  Instruction *Inst;
941  if (Action)
942  Inst = Action(Op0, ActionOp1, I, *this);
943  else {
944  // This action joins two actions together. The RHS of this action is
945  // simply the last action we processed, we saved the LHS action index in
946  // the joining action.
947  size_t SelectRHSIdx = i - 1;
948  Value *SelectRHS = UDivActions[SelectRHSIdx].FoldResult;
949  size_t SelectLHSIdx = UDivActions[i].SelectLHSIdx;
950  Value *SelectLHS = UDivActions[SelectLHSIdx].FoldResult;
951  Inst = SelectInst::Create(cast<SelectInst>(ActionOp1)->getCondition(),
952  SelectLHS, SelectRHS);
953  }
954 
955  // If this is the last action to process, return it to the InstCombiner.
956  // Otherwise, we insert it before the UDiv and record it so that we may
957  // use it as part of a joining action (i.e., a SelectInst).
958  if (e - i != 1) {
959  Inst->insertBefore(&I);
960  UDivActions[i].FoldResult = Inst;
961  } else
962  return Inst;
963  }
964 
965  return nullptr;
966 }
class_match< Value > m_Value()
m_Value() - Match an arbitrary value and ignore it.
Definition: PatternMatch.h:70
This class represents zero extension of integer types.
Value * SimplifyVectorOp(BinaryOperator &Inst)
Makes transformation of binary operation specific for vector types.
class_match< Constant > m_Constant()
Definition: PatternMatch.h:78
static RegisterPass< NVPTXAllocaHoisting > X("alloca-hoisting","Hoisting alloca instructions in non-entry ""blocks to the entry block")
bool match(Val *V, const Pattern &P)
Definition: PatternMatch.h:42
static cl::opt< ActionType > Action(cl::desc("Action to perform:"), cl::init(AC_Assemble), cl::values(clEnumValN(AC_AsLex,"as-lex","Lex tokens from a .s file"), clEnumValN(AC_Assemble,"assemble","Assemble a .s file (default)"), clEnumValN(AC_Disassemble,"disassemble","Disassemble strings of hex bytes"), clEnumValN(AC_MDisassemble,"mdis","Marked up disassembly of strings of hex bytes"), clEnumValEnd))
Instruction * commonIDivTransforms(BinaryOperator &I)
Common integer divide transforms.
static SelectInst * Create(Value *C, Value *S1, Value *S2, const Twine &NameStr="", Instruction *InsertBefore=nullptr)
BinaryOp_match< LHS, RHS, Instruction::LShr > m_LShr(const LHS &L, const RHS &R)
Definition: PatternMatch.h:491
void insertBefore(Instruction *InsertPos)
Definition: Instruction.cpp:82
LLVM Constant Representation.
Definition: Constant.h:41
Instruction * ReplaceInstUsesWith(Instruction &I, Value *V)
Definition: InstCombine.h:282
Value * getOperand(unsigned i) const
Definition: User.h:90
static size_t visitUDivOperand(Value *Op0, Value *Op1, const BinaryOperator &I, SmallVectorImpl< UDivFoldAction > &Actions, unsigned Depth=0)
BuilderTy * Builder
Definition: InstCombine.h:102
Type * getType() const
Definition: Value.h:215
bool isExact() const
isExact - Determine whether the exact flag is set.
Value * CreateUDiv(Value *LHS, Value *RHS, const Twine &Name="", bool isExact=false)
Definition: IRBuilder.h:698
static Constant * getShl(Constant *C1, Constant *C2, bool HasNUW=false, bool HasNSW=false)
Definition: Constants.cpp:2173
const DataLayout * DL
Definition: InstCombine.h:89
LLVM Value Representation.
Definition: Value.h:69
Value * SimplifyUDivInst(Value *LHS, Value *RHS, const DataLayout *TD=nullptr, const TargetLibraryInfo *TLI=nullptr, const DominatorTree *DT=nullptr)
static Value * dyn_castZExtVal(Value *V, Type *Ty)
Instruction * InstCombiner::visitUIToFP ( CastInst CI)

Definition at line 1398 of file InstCombineCasts.cpp.

1398  {
1399  return commonCastTransforms(CI);
1400 }
Instruction * commonCastTransforms(CastInst &CI)
Implement the transforms common to all CastInst visitors.
Instruction * llvm::InstVisitor< InstCombiner , Instruction * >::visitUIToFPInst ( UIToFPInst I)
inlineinherited

Definition at line 189 of file InstVisitor.h.

189 { DELEGATE(CastInst);}
#define DELEGATE(CLASS_TO_VISIT)
Definition: InstVisitor.h:30
Instruction * llvm::InstVisitor< InstCombiner , Instruction * >::visitUnaryInstruction ( UnaryInstruction I)
inlineinherited

Definition at line 235 of file InstVisitor.h.

#define DELEGATE(CLASS_TO_VISIT)
Definition: InstVisitor.h:30
Instruction * llvm::InstVisitor< InstCombiner , Instruction * >::visitUnreachableInst ( UnreachableInst I)
inlineinherited

Definition at line 171 of file InstVisitor.h.

171 { DELEGATE(TerminatorInst);}
#define DELEGATE(CLASS_TO_VISIT)
Definition: InstVisitor.h:30
Instruction * InstCombiner::visitURem ( BinaryOperator I)

Definition at line 1226 of file InstCombineMulDivRem.cpp.

1226  {
1227  Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
1228 
1229  if (Value *V = SimplifyVectorOp(I))
1230  return ReplaceInstUsesWith(I, V);
1231 
1232  if (Value *V = SimplifyURemInst(Op0, Op1, DL))
1233  return ReplaceInstUsesWith(I, V);
1234 
1235  if (Instruction *common = commonIRemTransforms(I))
1236  return common;
1237 
1238  // (zext A) urem (zext B) --> zext (A urem B)
1239  if (ZExtInst *ZOp0 = dyn_cast<ZExtInst>(Op0))
1240  if (Value *ZOp1 = dyn_castZExtVal(Op1, ZOp0->getSrcTy()))
1241  return new ZExtInst(Builder->CreateURem(ZOp0->getOperand(0), ZOp1),
1242  I.getType());
1243 
1244  // X urem Y -> X and Y-1, where Y is a power of 2,
1245  if (isKnownToBeAPowerOfTwo(Op1, /*OrZero*/true)) {
1247  Value *Add = Builder->CreateAdd(Op1, N1);
1248  return BinaryOperator::CreateAnd(Op0, Add);
1249  }
1250 
1251  // 1 urem X -> zext(X != 1)
1252  if (match(Op0, m_One())) {
1253  Value *Cmp = Builder->CreateICmpNE(Op1, Op0);
1254  Value *Ext = Builder->CreateZExt(Cmp, I.getType());
1255  return ReplaceInstUsesWith(I, Ext);
1256  }
1257 
1258  return nullptr;
1259 }
This class represents zero extension of integer types.
Value * SimplifyVectorOp(BinaryOperator &Inst)
Makes transformation of binary operation specific for vector types.
Instruction * commonIRemTransforms(BinaryOperator &I)
Common integer remainder transforms.
bool match(Val *V, const Pattern &P)
Definition: PatternMatch.h:42
Value * SimplifyURemInst(Value *LHS, Value *RHS, const DataLayout *TD=nullptr, const TargetLibraryInfo *TLI=nullptr, const DominatorTree *DT=nullptr)
Value * CreateAdd(Value *LHS, Value *RHS, const Twine &Name="", bool HasNUW=false, bool HasNSW=false)
Definition: IRBuilder.h:632
LLVM Constant Representation.
Definition: Constant.h:41
Instruction * ReplaceInstUsesWith(Instruction &I, Value *V)
Definition: InstCombine.h:282
Value * getOperand(unsigned i) const
Definition: User.h:90
static Constant * getAllOnesValue(Type *Ty)
Get the all ones value.
Definition: Constants.cpp:185
BuilderTy * Builder
Definition: InstCombine.h:102
Value * CreateZExt(Value *V, Type *DestTy, const Twine &Name="")
Definition: IRBuilder.h:1097
Type * getType() const
Definition: Value.h:215
bool isKnownToBeAPowerOfTwo(Value *V, bool OrZero=false, unsigned Depth=0)
Value * CreateURem(Value *LHS, Value *RHS, const Twine &Name="")
Definition: IRBuilder.h:730
Value * CreateICmpNE(Value *LHS, Value *RHS, const Twine &Name="")
Definition: IRBuilder.h:1250
const DataLayout * DL
Definition: InstCombine.h:89
LLVM Value Representation.
Definition: Value.h:69
cst_pred_ty< is_one > m_One()
m_One() - Match an integer 1 or a vector with all elements equal to 1.
Definition: PatternMatch.h:257
static Value * dyn_castZExtVal(Value *V, Type *Ty)
Instruction * llvm::InstVisitor< InstCombiner , Instruction * >::visitVAArgInst ( VAArgInst I)
inlineinherited

Definition at line 196 of file InstVisitor.h.

196 { DELEGATE(UnaryInstruction);}
#define DELEGATE(CLASS_TO_VISIT)
Definition: InstVisitor.h:30
Instruction * llvm::InstVisitor< InstCombiner , Instruction * >::visitVACopyInst ( VACopyInst I)
inlineinherited

Definition at line 215 of file InstVisitor.h.

215 { DELEGATE(IntrinsicInst); }
#define DELEGATE(CLASS_TO_VISIT)
Definition: InstVisitor.h:30
Instruction * llvm::InstVisitor< InstCombiner , Instruction * >::visitVAEndInst ( VAEndInst I)
inlineinherited

Definition at line 214 of file InstVisitor.h.

214 { DELEGATE(IntrinsicInst); }
#define DELEGATE(CLASS_TO_VISIT)
Definition: InstVisitor.h:30
Instruction * llvm::InstVisitor< InstCombiner , Instruction * >::visitVAStartInst ( VAStartInst I)
inlineinherited

Definition at line 213 of file InstVisitor.h.

213 { DELEGATE(IntrinsicInst); }
#define DELEGATE(CLASS_TO_VISIT)
Definition: InstVisitor.h:30
Instruction * InstCombiner::visitXor ( BinaryOperator I)

Definition at line 2221 of file InstCombineAndOrXor.cpp.

2221  {
2222  bool Changed = SimplifyAssociativeOrCommutative(I);
2223  Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
2224 
2225  if (Value *V = SimplifyVectorOp(I))
2226  return ReplaceInstUsesWith(I, V);
2227 
2228  if (Value *V = SimplifyXorInst(Op0, Op1, DL))
2229  return ReplaceInstUsesWith(I, V);
2230 
2231  // (A&B)^(A&C) -> A&(B^C) etc
2233  return ReplaceInstUsesWith(I, V);
2234 
2235  // See if we can simplify any instructions used by the instruction whose sole
2236  // purpose is to compute bits we don't care about.
2238  return &I;
2239 
2240  // Is this a ~ operation?
2241  if (Value *NotOp = dyn_castNotVal(&I)) {
2242  if (BinaryOperator *Op0I = dyn_cast<BinaryOperator>(NotOp)) {
2243  if (Op0I->getOpcode() == Instruction::And ||
2244  Op0I->getOpcode() == Instruction::Or) {
2245  // ~(~X & Y) --> (X | ~Y) - De Morgan's Law
2246  // ~(~X | Y) === (X & ~Y) - De Morgan's Law
2247  if (dyn_castNotVal(Op0I->getOperand(1)))
2248  Op0I->swapOperands();
2249  if (Value *Op0NotVal = dyn_castNotVal(Op0I->getOperand(0))) {
2250  Value *NotY =
2251  Builder->CreateNot(Op0I->getOperand(1),
2252  Op0I->getOperand(1)->getName()+".not");
2253  if (Op0I->getOpcode() == Instruction::And)
2254  return BinaryOperator::CreateOr(Op0NotVal, NotY);
2255  return BinaryOperator::CreateAnd(Op0NotVal, NotY);
2256  }
2257 
2258  // ~(X & Y) --> (~X | ~Y) - De Morgan's Law
2259  // ~(X | Y) === (~X & ~Y) - De Morgan's Law
2260  if (isFreeToInvert(Op0I->getOperand(0)) &&
2261  isFreeToInvert(Op0I->getOperand(1))) {
2262  Value *NotX =
2263  Builder->CreateNot(Op0I->getOperand(0), "notlhs");
2264  Value *NotY =
2265  Builder->CreateNot(Op0I->getOperand(1), "notrhs");
2266  if (Op0I->getOpcode() == Instruction::And)
2267  return BinaryOperator::CreateOr(NotX, NotY);
2268  return BinaryOperator::CreateAnd(NotX, NotY);
2269  }
2270 
2271  } else if (Op0I->getOpcode() == Instruction::AShr) {
2272  // ~(~X >>s Y) --> (X >>s Y)
2273  if (Value *Op0NotVal = dyn_castNotVal(Op0I->getOperand(0)))
2274  return BinaryOperator::CreateAShr(Op0NotVal, Op0I->getOperand(1));
2275  }
2276  }
2277  }
2278 
2279 
2280  if (ConstantInt *RHS = dyn_cast<ConstantInt>(Op1)) {
2281  if (RHS->isOne() && Op0->hasOneUse())
2282  // xor (cmp A, B), true = not (cmp A, B) = !cmp A, B
2283  if (CmpInst *CI = dyn_cast<CmpInst>(Op0))
2284  return CmpInst::Create(CI->getOpcode(),
2285  CI->getInversePredicate(),
2286  CI->getOperand(0), CI->getOperand(1));
2287 
2288  // fold (xor(zext(cmp)), 1) and (xor(sext(cmp)), -1) to ext(!cmp).
2289  if (CastInst *Op0C = dyn_cast<CastInst>(Op0)) {
2290  if (CmpInst *CI = dyn_cast<CmpInst>(Op0C->getOperand(0))) {
2291  if (CI->hasOneUse() && Op0C->hasOneUse()) {
2292  Instruction::CastOps Opcode = Op0C->getOpcode();
2293  if ((Opcode == Instruction::ZExt || Opcode == Instruction::SExt) &&
2294  (RHS == ConstantExpr::getCast(Opcode, Builder->getTrue(),
2295  Op0C->getDestTy()))) {
2296  CI->setPredicate(CI->getInversePredicate());
2297  return CastInst::Create(Opcode, CI, Op0C->getType());
2298  }
2299  }
2300  }
2301  }
2302 
2303  if (BinaryOperator *Op0I = dyn_cast<BinaryOperator>(Op0)) {
2304  // ~(c-X) == X-c-1 == X+(-c-1)
2305  if (Op0I->getOpcode() == Instruction::Sub && RHS->isAllOnesValue())
2306  if (Constant *Op0I0C = dyn_cast<Constant>(Op0I->getOperand(0))) {
2307  Constant *NegOp0I0C = ConstantExpr::getNeg(Op0I0C);
2308  Constant *ConstantRHS = ConstantExpr::getSub(NegOp0I0C,
2309  ConstantInt::get(I.getType(), 1));
2310  return BinaryOperator::CreateAdd(Op0I->getOperand(1), ConstantRHS);
2311  }
2312 
2313  if (ConstantInt *Op0CI = dyn_cast<ConstantInt>(Op0I->getOperand(1))) {
2314  if (Op0I->getOpcode() == Instruction::Add) {
2315  // ~(X-c) --> (-c-1)-X
2316  if (RHS->isAllOnesValue()) {
2317  Constant *NegOp0CI = ConstantExpr::getNeg(Op0CI);
2318  return BinaryOperator::CreateSub(
2319  ConstantExpr::getSub(NegOp0CI,
2320  ConstantInt::get(I.getType(), 1)),
2321  Op0I->getOperand(0));
2322  } else if (RHS->getValue().isSignBit()) {
2323  // (X + C) ^ signbit -> (X + C + signbit)
2324  Constant *C = Builder->getInt(RHS->getValue() + Op0CI->getValue());
2325  return BinaryOperator::CreateAdd(Op0I->getOperand(0), C);
2326 
2327  }
2328  } else if (Op0I->getOpcode() == Instruction::Or) {
2329  // (X|C1)^C2 -> X^(C1|C2) iff X&~C1 == 0
2330  if (MaskedValueIsZero(Op0I->getOperand(0), Op0CI->getValue())) {
2331  Constant *NewRHS = ConstantExpr::getOr(Op0CI, RHS);
2332  // Anything in both C1 and C2 is known to be zero, remove it from
2333  // NewRHS.
2334  Constant *CommonBits = ConstantExpr::getAnd(Op0CI, RHS);
2335  NewRHS = ConstantExpr::getAnd(NewRHS,
2336  ConstantExpr::getNot(CommonBits));
2337  Worklist.Add(Op0I);
2338  I.setOperand(0, Op0I->getOperand(0));
2339  I.setOperand(1, NewRHS);
2340  return &I;
2341  }
2342  } else if (Op0I->getOpcode() == Instruction::LShr) {
2343  // ((X^C1) >> C2) ^ C3 -> (X>>C2) ^ ((C1>>C2)^C3)
2344  // E1 = "X ^ C1"
2345  BinaryOperator *E1;
2346  ConstantInt *C1;
2347  if (Op0I->hasOneUse() &&
2348  (E1 = dyn_cast<BinaryOperator>(Op0I->getOperand(0))) &&
2349  E1->getOpcode() == Instruction::Xor &&
2350  (C1 = dyn_cast<ConstantInt>(E1->getOperand(1)))) {
2351  // fold (C1 >> C2) ^ C3
2352  ConstantInt *C2 = Op0CI, *C3 = RHS;
2353  APInt FoldConst = C1->getValue().lshr(C2->getValue());
2354  FoldConst ^= C3->getValue();
2355  // Prepare the two operands.
2356  Value *Opnd0 = Builder->CreateLShr(E1->getOperand(0), C2);
2357  Opnd0->takeName(Op0I);
2358  cast<Instruction>(Opnd0)->setDebugLoc(I.getDebugLoc());
2359  Value *FoldVal = ConstantInt::get(Opnd0->getType(), FoldConst);
2360 
2361  return BinaryOperator::CreateXor(Opnd0, FoldVal);
2362  }
2363  }
2364  }
2365  }
2366 
2367  // Try to fold constant and into select arguments.
2368  if (SelectInst *SI = dyn_cast<SelectInst>(Op0))
2369  if (Instruction *R = FoldOpIntoSelect(I, SI))
2370  return R;
2371  if (isa<PHINode>(Op0))
2372  if (Instruction *NV = FoldOpIntoPhi(I))
2373  return NV;
2374  }
2375 
2376  BinaryOperator *Op1I = dyn_cast<BinaryOperator>(Op1);
2377  if (Op1I) {
2378  Value *A, *B;
2379  if (match(Op1I, m_Or(m_Value(A), m_Value(B)))) {
2380  if (A == Op0) { // B^(B|A) == (A|B)^B
2381  Op1I->swapOperands();
2382  I.swapOperands();
2383  std::swap(Op0, Op1);
2384  } else if (B == Op0) { // B^(A|B) == (A|B)^B
2385  I.swapOperands(); // Simplified below.
2386  std::swap(Op0, Op1);
2387  }
2388  } else if (match(Op1I, m_And(m_Value(A), m_Value(B))) &&
2389  Op1I->hasOneUse()){
2390  if (A == Op0) { // A^(A&B) -> A^(B&A)
2391  Op1I->swapOperands();
2392  std::swap(A, B);
2393  }
2394  if (B == Op0) { // A^(B&A) -> (B&A)^A
2395  I.swapOperands(); // Simplified below.
2396  std::swap(Op0, Op1);
2397  }
2398  }
2399  }
2400 
2401  BinaryOperator *Op0I = dyn_cast<BinaryOperator>(Op0);
2402  if (Op0I) {
2403  Value *A, *B;
2404  if (match(Op0I, m_Or(m_Value(A), m_Value(B))) &&
2405  Op0I->hasOneUse()) {
2406  if (A == Op1) // (B|A)^B == (A|B)^B
2407  std::swap(A, B);
2408  if (B == Op1) // (A|B)^B == A & ~B
2409  return BinaryOperator::CreateAnd(A, Builder->CreateNot(Op1));
2410  } else if (match(Op0I, m_And(m_Value(A), m_Value(B))) &&
2411  Op0I->hasOneUse()){
2412  if (A == Op1) // (A&B)^A -> (B&A)^A
2413  std::swap(A, B);
2414  if (B == Op1 && // (B&A)^A == ~B & A
2415  !isa<ConstantInt>(Op1)) { // Canonical form is (B&C)^C
2416  return BinaryOperator::CreateAnd(Builder->CreateNot(A), Op1);
2417  }
2418  }
2419  }
2420 
2421  // (X >> Z) ^ (Y >> Z) -> (X^Y) >> Z for all shifts.
2422  if (Op0I && Op1I && Op0I->isShift() &&
2423  Op0I->getOpcode() == Op1I->getOpcode() &&
2424  Op0I->getOperand(1) == Op1I->getOperand(1) &&
2425  (Op0I->hasOneUse() || Op1I->hasOneUse())) {
2426  Value *NewOp =
2427  Builder->CreateXor(Op0I->getOperand(0), Op1I->getOperand(0),
2428  Op0I->getName());
2429  return BinaryOperator::Create(Op1I->getOpcode(), NewOp,
2430  Op1I->getOperand(1));
2431  }
2432 
2433  if (Op0I && Op1I) {
2434  Value *A, *B, *C, *D;
2435  // (A & B)^(A | B) -> A ^ B
2436  if (match(Op0I, m_And(m_Value(A), m_Value(B))) &&
2437  match(Op1I, m_Or(m_Value(C), m_Value(D)))) {
2438  if ((A == C && B == D) || (A == D && B == C))
2439  return BinaryOperator::CreateXor(A, B);
2440  }
2441  // (A | B)^(A & B) -> A ^ B
2442  if (match(Op0I, m_Or(m_Value(A), m_Value(B))) &&
2443  match(Op1I, m_And(m_Value(C), m_Value(D)))) {
2444  if ((A == C && B == D) || (A == D && B == C))
2445  return BinaryOperator::CreateXor(A, B);
2446  }
2447  }
2448 
2449  // (icmp1 A, B) ^ (icmp2 A, B) --> (icmp3 A, B)
2450  if (ICmpInst *RHS = dyn_cast<ICmpInst>(I.getOperand(1)))
2451  if (ICmpInst *LHS = dyn_cast<ICmpInst>(I.getOperand(0)))
2452  if (PredicatesFoldable(LHS->getPredicate(), RHS->getPredicate())) {
2453  if (LHS->getOperand(0) == RHS->getOperand(1) &&
2454  LHS->getOperand(1) == RHS->getOperand(0))
2455  LHS->swapOperands();
2456  if (LHS->getOperand(0) == RHS->getOperand(0) &&
2457  LHS->getOperand(1) == RHS->getOperand(1)) {
2458  Value *Op0 = LHS->getOperand(0), *Op1 = LHS->getOperand(1);
2459  unsigned Code = getICmpCode(LHS) ^ getICmpCode(RHS);
2460  bool isSigned = LHS->isSigned() || RHS->isSigned();
2461  return ReplaceInstUsesWith(I,
2462  getNewICmpValue(isSigned, Code, Op0, Op1,
2463  Builder));
2464  }
2465  }
2466 
2467  // fold (xor (cast A), (cast B)) -> (cast (xor A, B))
2468  if (CastInst *Op0C = dyn_cast<CastInst>(Op0)) {
2469  if (CastInst *Op1C = dyn_cast<CastInst>(Op1))
2470  if (Op0C->getOpcode() == Op1C->getOpcode()) { // same cast kind?
2471  Type *SrcTy = Op0C->getOperand(0)->getType();
2472  if (SrcTy == Op1C->getOperand(0)->getType() && SrcTy->isIntegerTy() &&
2473  // Only do this if the casts both really cause code to be generated.
2474  ShouldOptimizeCast(Op0C->getOpcode(), Op0C->getOperand(0),
2475  I.getType()) &&
2476  ShouldOptimizeCast(Op1C->getOpcode(), Op1C->getOperand(0),
2477  I.getType())) {
2478  Value *NewOp = Builder->CreateXor(Op0C->getOperand(0),
2479  Op1C->getOperand(0), I.getName());
2480  return CastInst::Create(Op0C->getOpcode(), NewOp, I.getType());
2481  }
2482  }
2483  }
2484 
2485  return Changed ? &I : nullptr;
2486 }
Value * CreateLShr(Value *LHS, Value *RHS, const Twine &Name="", bool isExact=false)
Definition: IRBuilder.h:770
Instruction * FoldOpIntoPhi(Instruction &I)
BinaryOp_match< LHS, RHS, Instruction::And > m_And(const LHS &L, const RHS &R)
Definition: PatternMatch.h:467
class_match< Value > m_Value()
m_Value() - Match an arbitrary value and ignore it.
Definition: PatternMatch.h:70
Abstract base class of comparison instructions.
Definition: InstrTypes.h:654
bool ShouldOptimizeCast(Instruction::CastOps opcode, const Value *V, Type *Ty)
unsigned getICmpCode(const ICmpInst *ICI, bool InvertPred=false)
Value * SimplifyVectorOp(BinaryOperator &Inst)
Makes transformation of binary operation specific for vector types.
void Add(Instruction *I)
Instruction * FoldOpIntoSelect(Instruction &Op, SelectInst *SI)
static Constant * getSub(Constant *C1, Constant *C2, bool HasNUW=false, bool HasNSW=false)
Definition: Constants.cpp:2113
StringRef getName() const
Definition: Value.cpp:168
bool match(Val *V, const Pattern &P)
Definition: PatternMatch.h:42
Value * CreateXor(Value *LHS, Value *RHS, const Twine &Name="")
Definition: IRBuilder.h:838
Base class of casting instructions.
Definition: InstrTypes.h:387
const APInt & getValue() const
Return the constant's value.
Definition: Constants.h:105
APInt LLVM_ATTRIBUTE_UNUSED_RESULT lshr(unsigned shiftAmt) const
Logical right-shift function.
Definition: APInt.cpp:1128
Value * SimplifyUsingDistributiveLaws(BinaryOperator &I)
void takeName(Value *V)
Definition: Value.cpp:242
static CmpInst * Create(OtherOps Op, unsigned short predicate, Value *S1, Value *S2, const Twine &Name="", Instruction *InsertBefore=nullptr)
Create a CmpInst.
BinaryOp_match< LHS, RHS, Instruction::Or > m_Or(const LHS &L, const RHS &R)
Definition: PatternMatch.h:473
ConstantInt * getTrue()
Get the constant value for i1 true.
Definition: IRBuilder.h:256
Value * CreateNot(Value *V, const Twine &Name="")
Definition: IRBuilder.h:885
LLVM Constant Representation.
Definition: Constant.h:41
Instruction * ReplaceInstUsesWith(Instruction &I, Value *V)
Definition: InstCombine.h:282
static Constant * getAnd(Constant *C1, Constant *C2)
Definition: Constants.cpp:2161
APInt Or(const APInt &LHS, const APInt &RHS)
Bitwise OR function for APInt.
Definition: APInt.h:1870
APInt Xor(const APInt &LHS, const APInt &RHS)
Bitwise XOR function for APInt.
Definition: APInt.h:1875
const DebugLoc & getDebugLoc() const
getDebugLoc - Return the debug location for this node as a DebugLoc.
Definition: Instruction.h:196
Represent an integer comparison operator.
Definition: Instructions.h:977
static bool isFreeToInvert(Value *V)
Value * getOperand(unsigned i) const
Definition: User.h:90
InstCombineWorklist Worklist
Worklist - All of the instructions that need to be simplified.
Definition: InstCombine.h:97
static Constant * getNot(Constant *C)
Definition: Constants.cpp:2096
BuilderTy * Builder
Definition: InstCombine.h:102
Value * SimplifyXorInst(Value *LHS, Value *RHS, const DataLayout *TD=nullptr, const TargetLibraryInfo *TLI=nullptr, const DominatorTree *DT=nullptr)
BinaryOps getOpcode() const
Definition: InstrTypes.h:326
bool SimplifyAssociativeOrCommutative(BinaryOperator &I)
Class for constant integers.
Definition: Constants.h:51
Type * getType() const
Definition: Value.h:215
static Constant * get(Type *Ty, uint64_t V, bool isSigned=false)
Definition: Constants.cpp:528
bool PredicatesFoldable(CmpInst::Predicate p1, CmpInst::Predicate p2)
void setOperand(unsigned i, Value *Val)
Definition: User.h:94
void swap(llvm::BitVector &LHS, llvm::BitVector &RHS)
Implement std::swap in terms of BitVector swap.
Definition: BitVector.h:590
Class for arbitrary precision integers.
Definition: APInt.h:75
bool isIntegerTy() const
Definition: Type.h:193
static BinaryOperator * Create(BinaryOps Op, Value *S1, Value *S2, const Twine &Name=Twine(), Instruction *InsertBefore=nullptr)
LLVM_ATTRIBUTE_UNUSED_RESULT std::enable_if< !is_simple_type< Y >::value, typename cast_retty< X, const Y >::ret_type >::type dyn_cast(const Y &Val)
Definition: Casting.h:265
APInt And(const APInt &LHS, const APInt &RHS)
Bitwise AND function for APInt.
Definition: APInt.h:1865
static Constant * getNeg(Constant *C, bool HasNUW=false, bool HasNSW=false)
Definition: Constants.cpp:2083
static CastInst * Create(Instruction::CastOps, Value *S, Type *Ty, const Twine &Name="", Instruction *InsertBefore=nullptr)
Construct any of the CastInst subclasses.
static Value * dyn_castNotVal(Value *V)
#define I(x, y, z)
Definition: MD5.cpp:54
static Constant * getOr(Constant *C1, Constant *C2)
Definition: Constants.cpp:2165
bool hasOneUse() const
Definition: Value.h:284
OtherOps getOpcode() const
Get the opcode casted to the right type.
Definition: InstrTypes.h:730
bool MaskedValueIsZero(Value *V, const APInt &Mask, unsigned Depth=0) const
Definition: InstCombine.h:323
const DataLayout * DL
Definition: InstCombine.h:89
LLVM Value Representation.
Definition: Value.h:69
static Value * getNewICmpValue(bool Sign, unsigned Code, Value *LHS, Value *RHS, InstCombiner::BuilderTy *Builder)
bool SimplifyDemandedInstructionBits(Instruction &Inst)
ConstantInt * getInt(const APInt &AI)
Get a constant integer value.
Definition: IRBuilder.h:292
static Constant * getCast(unsigned ops, Constant *C, Type *Ty)
Definition: Constants.cpp:1493
Instruction * InstCombiner::visitZExt ( ZExtInst CI)

Definition at line 760 of file InstCombineCasts.cpp.

760  {
761  // If this zero extend is only used by a truncate, let the truncate be
762  // eliminated before we try to optimize this zext.
763  if (CI.hasOneUse() && isa<TruncInst>(CI.user_back()))
764  return nullptr;
765 
766  // If one of the common conversion will work, do it.
767  if (Instruction *Result = commonCastTransforms(CI))
768  return Result;
769 
770  // See if we can simplify any instructions used by the input whose sole
771  // purpose is to compute bits we don't care about.
773  return &CI;
774 
775  Value *Src = CI.getOperand(0);
776  Type *SrcTy = Src->getType(), *DestTy = CI.getType();
777 
778  // Attempt to extend the entire input expression tree to the destination
779  // type. Only do this if the dest type is a simple type, don't convert the
780  // expression tree to something weird like i93 unless the source is also
781  // strange.
782  unsigned BitsToClear;
783  if ((DestTy->isVectorTy() || ShouldChangeType(SrcTy, DestTy)) &&
784  CanEvaluateZExtd(Src, DestTy, BitsToClear)) {
785  assert(BitsToClear < SrcTy->getScalarSizeInBits() &&
786  "Unreasonable BitsToClear");
787 
788  // Okay, we can transform this! Insert the new expression now.
789  DEBUG(dbgs() << "ICE: EvaluateInDifferentType converting expression type"
790  " to avoid zero extend: " << CI);
791  Value *Res = EvaluateInDifferentType(Src, DestTy, false);
792  assert(Res->getType() == DestTy);
793 
794  uint32_t SrcBitsKept = SrcTy->getScalarSizeInBits()-BitsToClear;
795  uint32_t DestBitSize = DestTy->getScalarSizeInBits();
796 
797  // If the high bits are already filled with zeros, just replace this
798  // cast with the result.
799  if (MaskedValueIsZero(Res, APInt::getHighBitsSet(DestBitSize,
800  DestBitSize-SrcBitsKept)))
801  return ReplaceInstUsesWith(CI, Res);
802 
803  // We need to emit an AND to clear the high bits.
805  APInt::getLowBitsSet(DestBitSize, SrcBitsKept));
806  return BinaryOperator::CreateAnd(Res, C);
807  }
808 
809  // If this is a TRUNC followed by a ZEXT then we are dealing with integral
810  // types and if the sizes are just right we can convert this into a logical
811  // 'and' which will be much cheaper than the pair of casts.
812  if (TruncInst *CSrc = dyn_cast<TruncInst>(Src)) { // A->B->C cast
813  // TODO: Subsume this into EvaluateInDifferentType.
814 
815  // Get the sizes of the types involved. We know that the intermediate type
816  // will be smaller than A or C, but don't know the relation between A and C.
817  Value *A = CSrc->getOperand(0);
818  unsigned SrcSize = A->getType()->getScalarSizeInBits();
819  unsigned MidSize = CSrc->getType()->getScalarSizeInBits();
820  unsigned DstSize = CI.getType()->getScalarSizeInBits();
821  // If we're actually extending zero bits, then if
822  // SrcSize < DstSize: zext(a & mask)
823  // SrcSize == DstSize: a & mask
824  // SrcSize > DstSize: trunc(a) & mask
825  if (SrcSize < DstSize) {
826  APInt AndValue(APInt::getLowBitsSet(SrcSize, MidSize));
827  Constant *AndConst = ConstantInt::get(A->getType(), AndValue);
828  Value *And = Builder->CreateAnd(A, AndConst, CSrc->getName()+".mask");
829  return new ZExtInst(And, CI.getType());
830  }
831 
832  if (SrcSize == DstSize) {
833  APInt AndValue(APInt::getLowBitsSet(SrcSize, MidSize));
834  return BinaryOperator::CreateAnd(A, ConstantInt::get(A->getType(),
835  AndValue));
836  }
837  if (SrcSize > DstSize) {
838  Value *Trunc = Builder->CreateTrunc(A, CI.getType());
839  APInt AndValue(APInt::getLowBitsSet(DstSize, MidSize));
840  return BinaryOperator::CreateAnd(Trunc,
841  ConstantInt::get(Trunc->getType(),
842  AndValue));
843  }
844  }
845 
846  if (ICmpInst *ICI = dyn_cast<ICmpInst>(Src))
847  return transformZExtICmp(ICI, CI);
848 
849  BinaryOperator *SrcI = dyn_cast<BinaryOperator>(Src);
850  if (SrcI && SrcI->getOpcode() == Instruction::Or) {
851  // zext (or icmp, icmp) --> or (zext icmp), (zext icmp) if at least one
852  // of the (zext icmp) will be transformed.
853  ICmpInst *LHS = dyn_cast<ICmpInst>(SrcI->getOperand(0));
854  ICmpInst *RHS = dyn_cast<ICmpInst>(SrcI->getOperand(1));
855  if (LHS && RHS && LHS->hasOneUse() && RHS->hasOneUse() &&
856  (transformZExtICmp(LHS, CI, false) ||
857  transformZExtICmp(RHS, CI, false))) {
858  Value *LCast = Builder->CreateZExt(LHS, CI.getType(), LHS->getName());
859  Value *RCast = Builder->CreateZExt(RHS, CI.getType(), RHS->getName());
860  return BinaryOperator::Create(Instruction::Or, LCast, RCast);
861  }
862  }
863 
864  // zext(trunc(X) & C) -> (X & zext(C)).
865  Constant *C;
866  Value *X;
867  if (SrcI &&
868  match(SrcI, m_OneUse(m_And(m_Trunc(m_Value(X)), m_Constant(C)))) &&
869  X->getType() == CI.getType())
870  return BinaryOperator::CreateAnd(X, ConstantExpr::getZExt(C, CI.getType()));
871 
872  // zext((trunc(X) & C) ^ C) -> ((X & zext(C)) ^ zext(C)).
873  Value *And;
874  if (SrcI && match(SrcI, m_OneUse(m_Xor(m_Value(And), m_Constant(C)))) &&
875  match(And, m_OneUse(m_And(m_Trunc(m_Value(X)), m_Specific(C)))) &&
876  X->getType() == CI.getType()) {
877  Constant *ZC = ConstantExpr::getZExt(C, CI.getType());
878  return BinaryOperator::CreateXor(Builder->CreateAnd(X, ZC), ZC);
879  }
880 
881  // zext (xor i1 X, true) to i32 --> xor (zext i1 X to i32), 1
882  if (SrcI && SrcI->hasOneUse() &&
883  SrcI->getType()->getScalarType()->isIntegerTy(1) &&
884  match(SrcI, m_Not(m_Value(X))) && (!X->hasOneUse() || !isa<CmpInst>(X))) {
885  Value *New = Builder->CreateZExt(X, CI.getType());
886  return BinaryOperator::CreateXor(New, ConstantInt::get(CI.getType(), 1));
887  }
888 
889  return nullptr;
890 }
BinaryOp_match< LHS, RHS, Instruction::And > m_And(const LHS &L, const RHS &R)
Definition: PatternMatch.h:467
class_match< Value > m_Value()
m_Value() - Match an arbitrary value and ignore it.
Definition: PatternMatch.h:70
This class represents zero extension of integer types.
Instruction * commonCastTransforms(CastInst &CI)
Implement the transforms common to all CastInst visitors.
static APInt getLowBitsSet(unsigned numBits, unsigned loBitsSet)
Get a value with low bits set.
Definition: APInt.h:526
class_match< Constant > m_Constant()
Definition: PatternMatch.h:78
static RegisterPass< NVPTXAllocaHoisting > X("alloca-hoisting","Hoisting alloca instructions in non-entry ""blocks to the entry block")
static bool CanEvaluateZExtd(Value *V, Type *Ty, unsigned &BitsToClear)
StringRef getName() const
Definition: Value.cpp:168
bool match(Val *V, const Pattern &P)
Definition: PatternMatch.h:42
BinaryOp_match< LHS, RHS, Instruction::Xor > m_Xor(const LHS &L, const RHS &R)
Definition: PatternMatch.h:479
CastClass_match< OpTy, Instruction::Trunc > m_Trunc(const OpTy &Op)
m_Trunc
Definition: PatternMatch.h:768
not_match< LHS > m_Not(const LHS &L)
Definition: PatternMatch.h:828
Value * CreateAnd(Value *LHS, Value *RHS, const Twine &Name="")
Definition: IRBuilder.h:806
This class represents a truncation of integer types.
Value * EvaluateInDifferentType(Value *V, Type *Ty, bool isSigned)
static APInt getHighBitsSet(unsigned numBits, unsigned hiBitsSet)
Get a value with high bits set.
Definition: APInt.h:508
OneUse_match< T > m_OneUse(const T &SubPattern)
Definition: PatternMatch.h:60
assert(Globals.size() > 1)
LLVM Constant Representation.
Definition: Constant.h:41
Instruction * ReplaceInstUsesWith(Instruction &I, Value *V)
Definition: InstCombine.h:282
APInt Or(const APInt &LHS, const APInt &RHS)
Bitwise OR function for APInt.
Definition: APInt.h:1870
specificval_ty m_Specific(const Value *V)
m_Specific - Match if we have a specific specified value.
Definition: PatternMatch.h:323
Represent an integer comparison operator.
Definition: Instructions.h:977
Value * getOperand(unsigned i) const
Definition: User.h:90
#define DEBUG(X)
BuilderTy * Builder
Definition: InstCombine.h:102
Instruction * transformZExtICmp(ICmpInst *ICI, Instruction &CI, bool DoXform=true)
BinaryOps getOpcode() const
Definition: InstrTypes.h:326
Definition: test.h:1
Value * CreateZExt(Value *V, Type *DestTy, const Twine &Name="")
Definition: IRBuilder.h:1097
unsigned getScalarSizeInBits() const LLVM_READONLY
Definition: Type.cpp:135
Type * getType() const
Definition: Value.h:215
Instruction * user_back()
Definition: Instruction.h:50
static Constant * get(Type *Ty, uint64_t V, bool isSigned=false)
Definition: Constants.cpp:528
raw_ostream & dbgs()
dbgs - Return a circular-buffered debug stream.
Class for arbitrary precision integers.
Definition: APInt.h:75
bool isIntegerTy() const
Definition: Type.h:193
static BinaryOperator * Create(BinaryOps Op, Value *S1, Value *S2, const Twine &Name=Twine(), Instruction *InsertBefore=nullptr)
LLVM_ATTRIBUTE_UNUSED_RESULT std::enable_if< !is_simple_type< Y >::value, typename cast_retty< X, const Y >::ret_type >::type dyn_cast(const Y &Val)
Definition: Casting.h:265
const Type * getScalarType() const LLVM_READONLY
Definition: Type.cpp:51
APInt And(const APInt &LHS, const APInt &RHS)
Bitwise AND function for APInt.
Definition: APInt.h:1865
static Constant * getZExt(Constant *C, Type *Ty)
Definition: Constants.cpp:1615
bool ShouldChangeType(Type *From, Type *To) const
bool hasOneUse() const
Definition: Value.h:284
bool MaskedValueIsZero(Value *V, const APInt &Mask, unsigned Depth=0) const
Definition: InstCombine.h:323
Value * CreateTrunc(Value *V, Type *DestTy, const Twine &Name="")
Definition: IRBuilder.h:1094
LLVM Value Representation.
Definition: Value.h:69
bool SimplifyDemandedInstructionBits(Instruction &Inst)
Instruction * llvm::InstVisitor< InstCombiner , Instruction * >::visitZExtInst ( ZExtInst I)
inlineinherited

Definition at line 183 of file InstVisitor.h.

183 { DELEGATE(CastInst);}
#define DELEGATE(CLASS_TO_VISIT)
Definition: InstVisitor.h:30
bool InstCombiner::WillNotOverflowSignedAdd ( Value LHS,
Value RHS 
)
private

WillNotOverflowSignedAdd - Return true if we can prove that: (sext (add LHS, RHS)) === (add (sext LHS), (sext RHS)) This basically requires proving that the add in the original type would not overflow to change the sign bit or have a carry out. TODO: Handle this for Vectors.

Definition at line 898 of file InstCombineAddSub.cpp.

898  {
899  // There are different heuristics we can use for this. Here are some simple
900  // ones.
901 
902  // If LHS and RHS each have at least two sign bits, the addition will look
903  // like
904  //
905  // XX..... +
906  // YY.....
907  //
908  // If the carry into the most significant position is 0, X and Y can't both
909  // be 1 and therefore the carry out of the addition is also 0.
910  //
911  // If the carry into the most significant position is 1, X and Y can't both
912  // be 0 and therefore the carry out of the addition is also 1.
913  //
914  // Since the carry into the most significant position is always equal to
915  // the carry out of the addition, there is no signed overflow.
916  if (ComputeNumSignBits(LHS) > 1 && ComputeNumSignBits(RHS) > 1)
917  return true;
918 
919  if (IntegerType *IT = dyn_cast<IntegerType>(LHS->getType())) {
920  int BitWidth = IT->getBitWidth();
921  APInt LHSKnownZero(BitWidth, 0);
922  APInt LHSKnownOne(BitWidth, 0);
923  computeKnownBits(LHS, LHSKnownZero, LHSKnownOne);
924 
925  APInt RHSKnownZero(BitWidth, 0);
926  APInt RHSKnownOne(BitWidth, 0);
927  computeKnownBits(RHS, RHSKnownZero, RHSKnownOne);
928 
929  // Addition of two 2's compliment numbers having opposite signs will never
930  // overflow.
931  if ((LHSKnownOne[BitWidth - 1] && RHSKnownZero[BitWidth - 1]) ||
932  (LHSKnownZero[BitWidth - 1] && RHSKnownOne[BitWidth - 1]))
933  return true;
934 
935  // Check if carry bit of addition will not cause overflow.
936  if (checkRippleForAdd(LHSKnownZero, RHSKnownZero))
937  return true;
938  if (checkRippleForAdd(RHSKnownZero, LHSKnownZero))
939  return true;
940  }
941  return false;
942 }
static bool checkRippleForAdd(const APInt &Op0KnownZero, const APInt &Op1KnownZero)
unsigned ComputeNumSignBits(Value *Op, unsigned Depth=0) const
Definition: InstCombine.h:327
static cl::opt< ITMode > IT(cl::desc("IT block support"), cl::Hidden, cl::init(DefaultIT), cl::ZeroOrMore, cl::values(clEnumValN(DefaultIT,"arm-default-it","Generate IT block based on arch"), clEnumValN(RestrictedIT,"arm-restrict-it","Disallow deprecated IT based on ARMv8"), clEnumValN(NoRestrictedIT,"arm-no-restrict-it","Allow IT blocks based on ARMv7"), clEnumValEnd))
Integer representation type.
Definition: DerivedTypes.h:37
Type * getType() const
Definition: Value.h:215
Class for arbitrary precision integers.
Definition: APInt.h:75
void computeKnownBits(Value *V, APInt &KnownZero, APInt &KnownOne, unsigned Depth=0) const
Definition: InstCombine.h:318
bool InstCombiner::WillNotOverflowUnsignedAdd ( Value LHS,
Value RHS 
)
private

WillNotOverflowUnsignedAdd - Return true if we can prove that: (zext (add LHS, RHS)) === (add (zext LHS), (zext RHS))

Definition at line 946 of file InstCombineAddSub.cpp.

946  {
947  // There are different heuristics we can use for this. Here is a simple one.
948  // If the sign bit of LHS and that of RHS are both zero, no unsigned wrap.
949  bool LHSKnownNonNegative, LHSKnownNegative;
950  bool RHSKnownNonNegative, RHSKnownNegative;
951  ComputeSignBit(LHS, LHSKnownNonNegative, LHSKnownNegative, DL, 0);
952  ComputeSignBit(RHS, RHSKnownNonNegative, RHSKnownNegative, DL, 0);
953  if (LHSKnownNonNegative && RHSKnownNonNegative)
954  return true;
955 
956  return false;
957 }
void ComputeSignBit(Value *V, bool &KnownZero, bool &KnownOne, const DataLayout *TD=nullptr, unsigned Depth=0)
const DataLayout * DL
Definition: InstCombine.h:89

Field Documentation

BuilderTy* llvm::InstCombiner::Builder

Definition at line 102 of file InstCombine.h.

const DataLayout* llvm::InstCombiner::DL
private

Definition at line 89 of file InstCombine.h.

char InstCombiner::ID = 0
static

Definition at line 104 of file InstCombine.h.

bool llvm::InstCombiner::MadeIRChange
private

Definition at line 91 of file InstCombine.h.

bool llvm::InstCombiner::MinimizeSize
private

Definition at line 93 of file InstCombine.h.

LibCallSimplifier* llvm::InstCombiner::Simplifier
private

Definition at line 92 of file InstCombine.h.

TargetLibraryInfo* llvm::InstCombiner::TLI
private

Definition at line 90 of file InstCombine.h.

InstCombineWorklist llvm::InstCombiner::Worklist

Worklist - All of the instructions that need to be simplified.

Definition at line 97 of file InstCombine.h.


The documentation for this class was generated from the following files: